"use client";

import {
  type WholesaleCustomer,
} from "@/actions/wholesale";

export type CustomerForm = {
  companyName: string;
  contactName: string;
  email: string;
  phone: string;
  accountStatus: string;
  creditLimit: number;
  depositsEnabled: boolean;
  depositThreshold: string;
  depositPercentage: string;
  orderEmail: string;
  invoiceEmail: string;
  adminNotes: string;
};

export const EMPTY_CUSTOMER_FORM: CustomerForm = {
  companyName: "",
  contactName: "",
  email: "",
  phone: "",
  accountStatus: "active",
  creditLimit: 0,
  depositsEnabled: false,
  depositThreshold: "",
  depositPercentage: "",
  orderEmail: "",
  invoiceEmail: "",
  adminNotes: "",
};

export function customerFormFromCustomer(c: WholesaleCustomer): CustomerForm {
  return {
    companyName: c.company_name ?? "",
    contactName: c.contact_name ?? "",
    email: c.email ?? "",
    phone: c.phone ?? "",
    accountStatus: c.account_status ?? "active",
    creditLimit: Number(c.credit_limit),
    depositsEnabled: c.deposits_enabled,
    depositThreshold: c.deposit_threshold?.toString() ?? "",
    depositPercentage: c.deposit_percentage?.toString() ?? "",
    orderEmail: c.order_email ?? "",
    invoiceEmail: c.invoice_email ?? "",
    adminNotes: c.admin_notes ?? "",
  };
}
