// ─── Raw entity (mirrors ClientDataEntity / NewsurveSurvesEntity) ───────────
export interface ClientData {
  id: number;
  user_id: number;
  location: number | null;
  standard_name: string | null;
  client_type: string;            // "Client" | "Surveillance" from paged-all
  country_name: number | null;
  company_name: string | null;
  contact_primary: string | null;
  designationpr: string | null;
  contact_second: string | null;
  designationsec: string | null;
  telephone: string | null;
  mobile_no: string | null;
  Address: string | null;
  email_id: string | null;
  company_sector: string | null;
  trade_license: string | null;
  vat_no: string | null;
  site_no: string | null;
  branch_no: string | null;
  head_office: string | null;
  employee_no: string | null;
  contact_finance: string | null;
  email_finance: string | null;
  status: number;
  created_at: string;
  updated_at: string;

  // 🔹 NEW (optional, additive) — extra fields paged-all now returns:
  /** Which legacy DB the row came from — ids collide across QRS/TQS. */
  source?: "QRS" | "TQS";
  /** Resolved standard names, e.g. ["ISO 9001:2015", ...]. */
  standard_names?: string[];
  /** Signed document path — only on clients__clientdatas rows. */
  signed_docs?: string | null;
  /** Signed document display name from the legacy CRM. */
  signeddocsname?: string | null;
}

// ─── Paged response (matches { rows, total, page, limit }) ──────────────────
export interface PagedClientsResponse {
  rows: ClientData[];
  total: number;
  page: number;
  limit: number;
}

// ─── Row shape used by the company search dropdown ─────────────────────────
export interface ClientSearchRow {
  id: number;
  company_name: string;
  client_type: string;
  contact_primary: string;
  mobile_no: string;
  telephone: string;
  email_id: string;
  Address: string;
}

// ═══════════════════════════════════════════════════════════════════════
// 🔹 ADDED — Clients module (list page) types. Nothing above was changed
// except the optional NEW fields inside ClientData.
// ═══════════════════════════════════════════════════════════════════════

export type ClientSource = "QRS" | "TQS";
export type ClientRecordType = "Client" | "Surveillance";
export type ClientTypeFilter = "All" | "Clients" | "Surveillance";

/** One row from GET /clients/paged-all — matches ClientRowDto on the Nest side. */
export interface ClientRow {
  id: number;
  /** Which legacy DB the row came from. Required — ids collide across QRS/TQS. */
  source: ClientSource;
  client_type: ClientRecordType;
  user_id: number;

  company_name: string | null;
  contact_primary: string | null;
  designationpr: string | null;
  contact_second: string | null;
  designationsec: string | null;

  telephone: string | null;
  mobile_no: string | null;
  email_id: string | null;
  Address: string | null;

  company_sector: string | null;
  /** JSON-encoded array of standard ids, e.g. "[\"5\",\"4\",\"6\"]" */
  standard_name: string | null;
  trade_license: string | null;
  vat_no: string | null;
  site_no: string | null;
  branch_no: string | null;
  head_office: string | null;
  employee_no: string | null;
  contact_finance: string | null;
  email_finance: string | null;
  location: number | null;
  country_name: number | null;

  /** Signed document path + display name — only on clients__clientdatas rows. */
  signed_docs?: string | null;
  signeddocsname?: string | null;

  /** Only present on clients__clientdatas rows, not on newsurve__surves. */
  status?: number | null;
  created_at?: string | null;
  updated_at?: string | null;
}

export interface ClientsPagedResponse {
  rows: ClientRow[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface ClientsPagedParams {
  page?: number;
  limit?: number;
  source?: "All" | ClientSource;
  type?: ClientTypeFilter;
  search?: string;
}

/** GET /clients/my-scope — tells you whether the legacy id mapping resolved. */
export interface ClientScope {
  scheme_user_id: number;
  can_view_all: boolean;
  qrs_user_id: number | null;
  tqs_user_id: number | null;
  note?: string;
}

/**
 * GET /clients/signed-doc/url — same shape as audit-report file/url.
 * The frontend just does window.open(url).
 */
export interface ClientSignedDocUrl {
  url: string;
  /** Actual file name on storage. */
  filename: string;
  /** signeddocsname from the legacy CRM (falls back to the file name). */
  docname: string;
}

/** GET /clients/details/:id — shaped for the audit-request auto-fill. */
export interface ClientDetails {
  client_ref_id: number;
  client_group: ClientSource;
  company_source: ClientRecordType;
  company_name: string;
  auditee_name: string;
  auditee_designation: string;
  auditee_contact: string;
  auditee_email: string;
  auditees: { name: string; designation: string }[];
  location: string;
  standard_name: string;
  accreditation: string;
  meta: Record<string, string | null>;
}