// ─── Entities ──────────────────────────────────────────────────────────────
export type CertType = "INITIAL" | "SURVEILLANCE" | "RECERTIFICATION";

export type CertStatus =
  | "pending_scan"
  | "active"
  | "superseded"
  | "expired"
  | "revoked"
  | "fake";

export type VerificationDomain = "local" | "international";

export interface Company {
  id: number;
  company_code: string;
  name: string;
  address: string;
  city: string;
  contact_person?: string;
  email?: string;
  mobile?: string;
}

export interface Standard {
  id: number;
  name: string;
  title: string;
}

export interface Certificate {
  id: number;
  certificate_no: string;
  cert_type: CertType;
  status: CertStatus;

  company: Company;
  standard: Standard;
  previous_certificate?: Certificate | null;

  company_name_snapshot: string;
  address_snapshot: string;
  city: string;
  country: string;
  standard_name: string;
  standard_short: string;
  scope_of_work: string;
  ea_codes: string;

  originally_registered: string;
  issue_date: string;
  expire_date: string;
  surveillance_audit_due?: string | null;
  recertification_due?: string | null;

  qrcode_token: string;
  fingerprint: string;
  verification_domain: VerificationDomain;

  scan_pdf_url: string | null;
  scan_uploaded_at: string | null;
  scan_uploaded_by: number | null;

  created_by: number | null;
  updated_by: number | null;
  created_at: string;
  updated_at: string;

  verification_url?: string;
}

// ─── DTOs ──────────────────────────────────────────────────────────────────
export interface CreateCertificateDto {
  company_id: number;
  standard_ids: number[];
  cert_type: CertType;
  city: string;
  country: string;
  scope_of_work: string;
  ea_codes: string;
  originally_registered?: string;
  issue_date?: string;
  expire_date?: string;
  surveillance_audit_due?: string;
  recertification_due?: string;
  verification_domain?: VerificationDomain;
}

export type UpdateCertificateDto = Partial<
  Omit<CreateCertificateDto, "company_id" | "standard_ids" | "cert_type">
> & {
  standard_id?: number;
  address_snapshot?: string;
  company_name_snapshot?: string;
  status?: CertStatus;
  scan_pdf_url?: string;
};

// ─── Create response ───────────────────────────────────────────────────────
export interface CreateCertificateResultItem {
  certificate: Certificate;
  qrCode: string;
  verification_url: string;
  action: "REUSED" | "GENERATED";
  source: "new_certificate" | "api_excel" | "new";
}

export interface CreateCertificateSummary {
  total: number;
  reused: number;
  generated: number;
}

/**
 * Backend returns either:
 *   - Single object {certificate, qrCode, verification_url, action, source}  (1 standard)
 *   - Array of items  (multiple standards)
 *   - Object with {summary, certificates: [...]}  (our updated controller)
 *
 * The mapper normalizes all three into the same shape below.
 */
export interface CreateCertificateResponse {
  summary: CreateCertificateSummary;
  certificates: CreateCertificateResultItem[];
}

// ─── Verify endpoint ───────────────────────────────────────────────────────
export interface VerifyCertificateResponse {
  valid: boolean;
  message?: string;
  status?: CertStatus;
  certificate_no?: string;
  cert_type?: CertType;
  issue_date?: string;
  expiry_date?: string;
  originally_registered?: string;
  company?: Company;
  standard?: Standard;
  scope_of_work?: string;
  ea_codes?: string;
  verification_domain?: VerificationDomain;
  digital_certificate_url?: string | null;
}

// ─── Bulk scan upload ──────────────────────────────────────────────────────
export interface BulkScanMatched {
  filename: string;
  certificate_no: string;
  certificate_id: number;
  scan_pdf_url?: string;
}

export interface BulkScanReview {
  filename: string;
  certificate_no: string;
  reason: string;
}

export interface BulkScanUnmatched {
  filename: string;
  reason: string;
}

export interface BulkScanResponse {
  matched: BulkScanMatched[];
  review: BulkScanReview[];
  unmatched: BulkScanUnmatched[];
  summary?: {
    total: number;
    matched: number;
    review: number;
    unmatched: number;
  };
}

// ─── Pagination ────────────────────────────────────────────────────────────
export interface CertificatePaginationMeta {
  total: number;
  page: number;
  limit: number;
  totalPages: number;
  hasNextPage: boolean;
  hasPrevPage: boolean;
}

export interface PaginatedCertificatesResponse {
  data: (Certificate & { qrCode?: string; verification_url?: string })[];
  total: number;
  page: number;
  lastPage: number;
}

// ─── Table row ─────────────────────────────────────────────────────────────
export interface CertificateRow {
  sno: number;
  id: number;
  certificate_no: string;
  company_name: string;
  company_id: number;
  standard_name: string;
  standard_short: string;
  cert_type: CertType;
  status: CertStatus;
  issue_date: string;
  expire_date: string;
  surveillance_audit_due?: string | null;
  recertification_due?: string | null;
  scan_pdf_url: string | null;
  qrcode_token: string;
  fingerprint: string;
  verification_domain: VerificationDomain;
  verification_url: string;
  qrCode?: string;
  created_at: string;
  updated_at: string;
  [key: string]: any;
}

// ─── Legacy excel cert (from /api/excel/search) ────────────────────────────
export interface LegacyExcelCert {
  id: number;
  cert_no: string;
  company_name: string;
  standard: string;
  orginally_reg?: string | null;
  issue_date?: string | null;
  expire_date?: string | null;
  status?: string;
}

export interface PaginatedLegacyExcelResponse {
  data: LegacyExcelCert[];
  total: number;
  page: number;
  limit: number;
  lastPage: number;
  hasNext: boolean;
  hasPrev: boolean;
}
