import type { Database } from "@xtablo/shared-types";

type AdminTableColumn = {
  id: string;
  label: string;
};

type AdminTableDefinition = {
  columns: AdminTableColumn[];
  editableColumns?: string[];
  id: string;
  label: string;
  primaryKey: string;
  select: string;
  source: keyof Database["public"]["Tables"];
};

export const adminTableRegistry: Record<string, AdminTableDefinition> = {
  profiles: {
    columns: [
      { id: "id", label: "ID" },
      { id: "email", label: "Email" },
      { id: "first_name", label: "First name" },
      { id: "last_name", label: "Last name" },
    ],
    editableColumns: ["first_name", "last_name"],
    id: "profiles",
    label: "Users",
    primaryKey: "id",
    select: "id,email,first_name,last_name",
    source: "profiles",
  },
  tablo_access: {
    columns: [
      { id: "tablo_id", label: "Tablo ID" },
      { id: "user_id", label: "User ID" },
      { id: "is_active", label: "Active" },
      { id: "is_admin", label: "Admin" },
    ],
    editableColumns: [],
    id: "tablo_access",
    label: "Tablo Access",
    primaryKey: "user_id",
    select: "tablo_id,user_id,is_active,is_admin",
    source: "tablo_access",
  },
};

type AdminDatasetDefinition = {
  description: string;
  id: string;
  label: string;
};

type AdminActionFieldDefinition = {
  id: string;
  label: string;
  placeholder?: string;
  required?: boolean;
};

type AdminActionDefinition = {
  description: string;
  fields: AdminActionFieldDefinition[];
  id: string;
  label: string;
};

export const adminDatasetRegistry: Record<string, AdminDatasetDefinition> = {
  profile_growth: {
    description: "New user creation trend over time.",
    id: "profile_growth",
    label: "User Growth",
  },
  plan_mix: {
    description: "Production users by current subscription plan.",
    id: "plan_mix",
    label: "Plan Mix",
  },
  tablo_access_mix: {
    description: "Current active, inactive, and admin access posture.",
    id: "tablo_access_mix",
    label: "Tablo Access Mix",
  },
};

export const adminActionRegistry: Record<string, AdminActionDefinition> = {
  deactivate_tablo_access: {
    description: "Disable a user's access to a tablo and log the operator reason.",
    fields: [
      { id: "tabloId", label: "Tablo ID", placeholder: "tablo_123", required: true },
      { id: "userId", label: "User ID", placeholder: "user_123", required: true },
      {
        id: "reason",
        label: "Reason",
        placeholder: "Explain why this access is being removed",
        required: true,
      },
    ],
    id: "deactivate_tablo_access",
    label: "Deactivate Tablo Access",
  },
  grant_tablo_admin: {
    description: "Promote an existing tablo member to admin and force active access.",
    fields: [
      { id: "tabloId", label: "Tablo ID", placeholder: "tablo_123", required: true },
      { id: "userId", label: "User ID", placeholder: "user_123", required: true },
      {
        id: "reason",
        label: "Reason",
        placeholder: "Explain why admin access is being granted",
        required: true,
      },
    ],
    id: "grant_tablo_admin",
    label: "Grant Tablo Admin",
  },
};

export function getAdminTableDefinition(tableId: string) {
  return adminTableRegistry[tableId] ?? null;
}

export function listAdminTables() {
  return Object.values(adminTableRegistry).map(({ id, label }) => ({ id, label }));
}

export function getAdminDatasetDefinition(datasetId: string) {
  return adminDatasetRegistry[datasetId] ?? null;
}

export function listAdminDatasets() {
  return Object.values(adminDatasetRegistry);
}

export function getAdminActionDefinition(actionId: string) {
  return adminActionRegistry[actionId] ?? null;
}

export function listAdminActions() {
  return Object.values(adminActionRegistry);
}

export function normalizeAdminRows(rows: unknown[]) {
  return rows as Record<string, unknown>[];
}
