export type DashboardStatus = "draft" | "published";

/**
 * One row from GET /v1/admin-console/dashboards.
 *
 * Mirrors the backend's list projection exactly (dashboards LEFT JOIN the
 * current dashboard_versions row) — nothing here is invented client-side.
 */
export type DashboardListItem = {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  status: DashboardStatus;
  /** GCS object path, NOT a URL. There is no cover-serving endpoint yet. */
  cover_image_path: string | null;
  current_version_id: string | null;
  created_by: string;
  created_at: string;
  updated_at: string;
  /** Live version's number — null until a first version is uploaded. */
  current_version_number: number | null;
  current_entry_point: string | null;
  current_size_bytes: string | number | null;
  current_file_count: number | null;
  current_version_created_at: string | null;
  /** Computed in SQL by the list endpoint (correlated EXISTS, not an N+1). */
  has_public_link?: boolean;
};

export type DashboardSort = "recent" | "name";
export type DashboardStatusFilter = "all" | DashboardStatus;

export type DashboardVersion = {
  id: string;
  dashboard_id: string;
  version_number: number;
  storage_prefix: string;
  entry_point: string;
  manifest: Record<string, unknown> | null;
  size_bytes: string | number;
  file_count: number;
  uploaded_by: string;
  created_at: string;
};

export type DashboardAccessGrant = {
  grant_id: string;
  console_user_id: string;
  granted_by: string;
  created_at: string;
  email: string;
  first_name: string | null;
  last_name: string | null;
  is_active: boolean;
};

/** Metadata only — the API never returns the token or either hash. */
export type DashboardPublicLink = {
  id: string;
  dashboard_id: string;
  expires_at: string | null;
  is_revoked: boolean;
  view_count: number;
  has_password: boolean;
  created_by: string;
  created_at: string;
};

/** Only ever returned once, at creation. */
export type CreatedPublicLink = {
  id: string;
  url: string;
  rawToken: string;
  expiresAt: string | null;
  hasPassword: boolean;
  note: string;
};

export type DashboardAuditEntry = {
  id: string;
  dashboard_id: string | null;
  action: string;
  actor_console_user_id: string | null;
  actor_email: string | null;
  metadata: Record<string, unknown> | null;
  created_at: string;
};

export type DashboardDetail = {
  dashboard: DashboardListItem;
  versions: DashboardVersion[];
  isDashboardAdmin: boolean;
  accessList?: DashboardAccessGrant[];
  publicLinks?: DashboardPublicLink[];
  /** Merged in by the route loader from GET /:id/audit. */
  audit?: DashboardAuditEntry[];
};

export type UploadedVersion = {
  version: DashboardVersion;
  warnings: string[];
  /** Candidate entry points for the override step. */
  htmlFiles: string[];
};
