import type { Company, CompanyRow } from "@/lib/api/types/company.types";
// ─────────────────────────────────────────────────────────────────────────────
// mapCompanyToRow
// Transforms raw API Company → CompanyRow (used in CompanyTable.refreshData)
// Mirrors: mapProposal() in proposal.service.ts
// ─────────────────────────────────────────────────────────────────────────────
export function mapCompanyToRow(company: Company, index: number): CompanyRow {
  return {
    sno:                index + 1,
    id:                 company.id,
    company_code:       company.company_code ?? "—",
    name:               company.name ?? "—",
    address:            company.address ?? "—",
    city:               company.city?.trim() ?? "—",
    contact_person:     company.contact_person ?? "—",
    designation:        company.designation ?? "—",
    email:              company.email ?? "—",
    mobile:             company.mobile ?? "—",
    telephone:          company.telephone ?? "—",
    fax:                company.fax ?? "—",
    validity:           company.validity ?? "—",
    certification_body: company.certification_body ?? "—",
     client_group:       company.client_group ?? "", 
    accreditation:      company.accreditation ?? "—",
    scope_of_work:      company.scope_of_work?.trim() ?? "—",
    reference_number:   company.reference_number ?? "—",
    standards:          company.standards ?? [],
    country:            company.country,
    created_at:         company.created_at,
    updated_at:         company.updated_at,
  };
}

// ─────────────────────────────────────────────────────────────────────────────
// mapCompaniesApiResponse
// Maps the full paginated API response array → CompanyRow[]
// ─────────────────────────────────────────────────────────────────────────────
export function mapCompaniesApiResponse(companies: Company[]): CompanyRow[] {
  return companies.map((c, idx) => mapCompanyToRow(c, idx));
}

// ─────────────────────────────────────────────────────────────────────────────
// mapColumnKeyToValue
// Maps a column key to the display value for a given row.
// Used by DynamicCompanyRow (mirrors mapColumnKeyToValue in ProposalTable).
// ─────────────────────────────────────────────────────────────────────────────
export function mapColumnKeyToValue(key: string, row: CompanyRow): any {
  const columnMap: Record<string, any> = {
    // ── Identity ────────────────────────────────────────────────────────────
    company_code:       row.company_code,
    name:               row.name,

    // ── Location ────────────────────────────────────────────────────────────
    city:               row.city,
    address:            row.address,

    // ── Contact ─────────────────────────────────────────────────────────────
    contact_person:     row.contact_person,
    designation:        row.designation,
    email:              row.email,
    mobile:             row.mobile,
    telephone:          row.telephone,
    fax:                row.fax !== "NONE" ? row.fax : "—",

    // ── Certification ────────────────────────────────────────────────────────
    validity:           row.validity,
    certification_body: row.certification_body,
    client_group: row.client_group,
    accreditation:      row.accreditation,
    reference_number:   row.reference_number,

    // ── Standards (rendered as badges in DynamicCompanyRow) ─────────────────
    standards:          row.standards,   // raw array — renderer handles display
    standards_count:    `${row.standards?.length ?? 0} standard(s)`,
    standards_names:    row.standards?.map((s) => s.name).join(", ") || "—",

    // ── Country ──────────────────────────────────────────────────────────────
    country:            row.country?.name ?? "—",
    country_code:       row.country?.code ?? "—",

    // ── Scope ────────────────────────────────────────────────────────────────
    scope_of_work:      row.scope_of_work,

    // ── Dates ────────────────────────────────────────────────────────────────
    created_at: row.created_at
      ? new Date(row.created_at)
          .toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric" })
          .replace(/ /g, "-")
      : "—",
    updated_at: row.updated_at
      ? new Date(row.updated_at)
          .toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric" })
          .replace(/ /g, "-")
      : "—",
  };

  return columnMap[key] ?? row[key] ?? "—";
}

// ─────────────────────────────────────────────────────────────────────────────
// formatCompanyForExport
// Flat object suitable for Excel/CSV export rows
// ─────────────────────────────────────────────────────────────────────────────
export function formatCompanyForExport(row: CompanyRow): Record<string, string | number> {
  return {
    "No.":               row.sno,
    "Company Code":      row.company_code,
    "Company Name":      row.name,
    "City":              row.city,
    "Address":           row.address,
    "Contact Person":    row.contact_person,
    "Designation":       row.designation,
    "Email":             row.email,
    "Mobile":            row.mobile,
    "Telephone":         row.telephone,
    "Fax":               row.fax !== "NONE" ? row.fax : "",
    "Validity":          row.validity,
    "Certification Body":row.certification_body,
    "Accreditation":     row.accreditation,
    "Standards":         row.standards?.map((s) => s.name).join(" | ") ?? "",
    "Country":           row.country?.name ?? "",
    "Scope of Work":     row.scope_of_work,
    "Registered":        row.created_at
      ? new Date(row.created_at).toLocaleDateString("en-GB")
      : "",
  };
}
