import type { AuthUser } from "@/types/auth";
import type { CreateCompanyPayload } from "@/services/companies.service";
import type { ProfileResponse, UpdateProfilePayload } from "@/types/profile";

import type { CompanyStep, FormData } from "./schema";

/** Last 9 digits (Ethiopian national significant number) for tolerant compare. */
export const phoneDigits = (p?: string | null) =>
  (p ?? "").replace(/\D/g, "").slice(-9);

export const samePhone = (a?: string | null, b?: string | null) => {
  const da = phoneDigits(a);
  return da.length === 9 && da === phoneDigits(b);
};

/** Mask all but the first 7 chars of an E.164 phone for display. */
export const maskPhone = (p: string) =>
  p.length > 4 ? `${p.slice(0, 7)}${"*".repeat(Math.max(0, p.length - 7))}` : p;

export function buildPayload(
  data: FormData,
  _user: AuthUser,
): CreateCompanyPayload {
  return {
    companyName: data.companyName,
    companyEmail: data.companyEmail,
    companyPhone: data.companyPhone,
    companyLocation: data.companyLocation,
    companyAddress: data.companyAddress,
    tin: data.tinNumber,
    vatNumber: data.vatNumber,
    fanNumber: data.fanNumber,
    attributes: {
      contactPersonName: data.contactPersonName,
      contactPersonPosition: data.contactPersonPosition || undefined,
      contactPersonEmail: data.contactPersonEmail || undefined,
      contactPersonPhone: data.contactPersonPhone,
      generalManagerName: data.generalManagerName,
      generalManagerEmail: data.generalManagerEmail,
      generalManagerPhone: data.generalManagerPhone,
      poaName: data.poaName || undefined,
      poaPhone: data.poaPhone || undefined,
      poaAddress: data.poaAddress || undefined,
      poaEmail: data.poaEmail || undefined,
      poaLocation: data.poaLocation || undefined,
    },
  };
}

/** Map one wizard step's form values to the profile-update payload it saves. */
export function stepPayload(
  step: CompanyStep,
  d: FormData,
): Partial<UpdateProfilePayload> {
  switch (step) {
    case "company":
      return {
        companyName: d.companyName,
        companyEmail: d.companyEmail,
        companyPhone: d.companyPhone,
        companyLocation: d.companyLocation,
        companyAddress: d.companyAddress,
        tin: d.tinNumber,
        vatNumber: d.vatNumber,
        fanNumber: d.fanNumber,
        licenceNumber: d.licenceNumber,
        statusDescription: d.statusDescription,
        dateRegistered: d.dateRegistered,
        renewedFrom: d.renewedFrom,
        renewalDate: d.renewalDate,
        renewedTo: d.renewedTo,
        region: d.region,
        zone: d.zone,
        woreda: d.woreda,
        kebele: d.kebele,
        houseNo: d.houseNo,
        etradePhone: d.companyPhone,
      };
    case "personnel":
      return {
        generalManagerName: d.generalManagerName,
        generalManagerEmail: d.generalManagerEmail,
        generalManagerPhone: d.generalManagerPhone,
      };
    case "contact":
      return {
        contactPersonName: d.contactPersonName,
        contactPersonPosition: d.contactPersonPosition || undefined,
        contactPersonEmail: d.contactPersonEmail || undefined,
        contactPersonPhone: d.contactPersonPhone,
      };
    case "poa":
      return {
        poaName: d.poaName || undefined,
        poaPhone: d.poaPhone || undefined,
        poaEmail: d.poaEmail || undefined,
        poaLocation: d.poaLocation || undefined,
        poaAddress: d.poaAddress || undefined,
      };
    default:
      return {};
  }
}

/** Seed the form from previously-saved profile data. */
export function toFormValues(p: ProfileResponse): FormData {
  // The draft placeholder TIN ("D…") shouldn't show as a real value.
  const tin = p.tinNumber && !p.tinNumber.startsWith("D") ? p.tinNumber : "";
  return {
    companyName: p.companyName ?? "",
    companyEmail: p.companyEmail ?? "",
    companyPhone: p.companyPhone ?? "",
    companyLocation: p.companyLocation ?? "",
    companyAddress: p.companyAddress ?? "",
    tinNumber: tin,
    vatNumber: p.vatNumber ?? "",
    fanNumber: p.fanNumber ?? "",
    licenceNumber: p.licenceNumber ?? "",
    statusDescription: p.statusDescription ?? "",
    dateRegistered: p.dateRegistered ?? "",
    renewedFrom: p.renewedFrom ?? "",
    renewalDate: p.renewalDate ?? "",
    renewedTo: p.renewedTo ?? "",
    region: p.region ?? "",
    zone: p.zone ?? "",
    woreda: p.woreda ?? "",
    kebele: p.kebele ?? "",
    houseNo: p.houseNo ?? "",
    contactPersonName: p.contactPersonName ?? "",
    contactPersonPosition: p.contactPersonPosition ?? "",
    contactPersonEmail: p.contactPersonEmail ?? "",
    contactPersonPhone: p.contactPersonPhone ?? "",
    generalManagerName: p.generalManagerName ?? "",
    generalManagerEmail: p.generalManagerEmail ?? "",
    generalManagerPhone: p.generalManagerPhone ?? "",
    poaName: p.poaName ?? "",
    poaPhone: p.poaPhone ?? "",
    poaAddress: p.poaAddress ?? "",
    poaEmail: p.poaEmail ?? "",
    poaLocation: p.poaLocation ?? "",
  };
}
