export type AuthUser = {
  id: string | number;
  username: string;
  fullName: string;
  role: string;
};

export type AuthSession = {
  user: AuthUser;
  accessToken: string;
  expiresAt: string;
};

export type ApiEnvelope<T> = {
  ok: boolean;
  data: T;
};

export type InvimaDataset = {
  key: string;
  id: string;
  name: string;
  defaultCategory: string;
};

export type InvimaCategory = {
  categoria: string;
  slug: string;
  total: number;
};

export type InvimaFilterOption = {
  value: string;
  total: number;
};

export type InvimaCatalogItem = {
  source_dataset_key: string;
  source_dataset_id: string;
  source_dataset_name: string;
  categoria_linea: string;
  categoria_canonica: string;
  categoria_slug: string;
  expediente?: string;
  registro_sanitario?: string;
  producto?: string;
  titular?: string;
  estado_registro?: string;
  fecha_expedicion?: string;
  fecha_vencimiento?: string;
  modalidad?: string;
  grupo?: string;
  marca?: string;
  principio_activo?: string;
  forma_farmaceutica?: string;
  presentacion_comercial?: string;
  atc?: string;
  rol_nombre?: string;
  rol_tipo?: string;
  ciudad_titular?: string;
  pais_titular?: string;
  fabricante?: string;
  importador?: string;
  vigencia?: string;
  score?: number;
};

export type InvimaCatalogResponse = {
  items: InvimaCatalogItem[];
  total: number;
  limit: number;
  offset: number;
};

export type InvimaFiltersResponse = {
  estados: InvimaFilterOption[];
  modalidades: InvimaFilterOption[];
  grupos: InvimaFilterOption[];
};

export type InvimaSyncSummary = {
  datasets: number;
  fetched: number;
  persisted: number;
  pages: number;
  failed?: number;
  requested?: number;
};

export type InvimaSyncResponse = {
  ok: boolean;
  alreadyRunning?: boolean;
  job?: InvimaSyncJob | null;
  summary?: InvimaSyncSummary;
  failed?: Array<{
    datasetKey: string;
    error: string;
  }>;
};

export type InvimaSyncJobDataset = {
  key: string;
  datasetKey: string;
  datasetId: string | null;
  name: string;
  status: 'pending' | 'running' | 'completed' | 'failed';
  fetched: number;
  persisted: number;
  pages: number;
  warnings: string[];
  error: string | null;
  startedAt: string | null;
  updatedAt: string;
  finishedAt: string | null;
};

export type InvimaSyncJob = {
  id: string;
  status: 'running' | 'completed' | 'failed';
  requested: number;
  completed: number;
  failed: number;
  fetched: number;
  persisted: number;
  pages: number;
  concurrency?: number;
  pageSize?: number;
  datasets: InvimaSyncJobDataset[];
  summary?: InvimaSyncSummary | null;
  error?: string | null;
  startedAt: string;
  updatedAt: string;
  finishedAt: string | null;
};

export type InvimaSyncDatasetStats = {
  key: string;
  id: string;
  name: string;
  localRows: number;
  selected: boolean;
};

export type InvimaSyncStats = {
  at: string;
  totals: {
    catalogRows: number;
    rawRows: number;
    categories: number;
    datasetsWithData: number;
    vigentes: number;
    vencidos: number;
    maxUpdatedAt: string | null;
  };
  datasets: InvimaSyncDatasetStats[];
};
