import { z } from "zod";

const ClientTypeSchema = z.enum(["company", "person", "pa", "condo"]);

const VatTypeSchema = z.object({
  id: z.number().nullish(),
  value: z.number().nullish(),
  description: z.string().nullish(),
  notes: z.string().nullish(),
  e_invoice: z.boolean().nullish(),
  ei_type: z.string().nullish(),
  ei_description: z.string().nullish(),
  editable: z.boolean().nullish(),
  is_disabled: z.boolean().nullish(),
});

const PaymentTermsTypeSchema = z.enum(["standard", "end_of_month"]);
const PaymentMethodTypeSchema = z.enum(["standard", "riba"]);
const PaymentAccountTypeSchema = z.enum(["standard", "bank"]);
const PaymentAccountSchema = z.object({
  id: z.number().nullish(),
  name: z.string().nullish(),
  type: PaymentAccountTypeSchema.optional(),
  iban: z.string().nullish(),
  sia: z.string().nullish(),
  cuc: z.string().nullish(),
  virtual: z.boolean().nullish(),
});

const PaymentMethodDetailsSchema = z.object({
  title: z.string().nullish(),
  description: z.string().nullish(),
});

const PaymentMethodSchema = z.object({
  id: z.number().nullish(),
  name: z.string().nullish(),
  type: PaymentMethodTypeSchema.optional(),
  is_default: z.boolean().nullish(),
  default_payment_account: PaymentAccountSchema.nullish(),
  details: z.array(PaymentMethodDetailsSchema).optional(),
  bank_iban: z.string().nullish(),
  bank_name: z.string().nullish(),
  bank_beneficiary: z.string().nullish(),
  ei_payment_method: z.string().nullish(),
});

export const ClientSchema = z.object({
  id: z.number().nullish(),
  code: z.string().nullish(),
  name: z.string().nullish(),
  type: ClientTypeSchema.nullish(),
  first_name: z.string().nullish(),
  last_name: z.string().nullish(),
  contact_person: z.string().nullish(),
  vat_number: z.string().nullish(),
  tax_code: z.string().nullish(),
  address_street: z.string().nullish(),
  address_postal_code: z.string().nullish(),
  address_city: z.string().nullish(),
  address_province: z.string().nullish(),
  address_extra: z.string().nullish(),
  country: z.string().nullish(),
  country_iso: z.string().nullish(),
  email: z.string().nullish(),
  certified_email: z.string().nullish(),
  phone: z.string().nullish(),
  fax: z.string().nullish(),
  notes: z.string().nullish(),
  default_vat: VatTypeSchema.nullish(),
  default_payment_terms: z.number().nullish(),
  default_payment_terms_type: PaymentTermsTypeSchema.optional(),
  default_payment_method: PaymentMethodSchema.optional(),
  bank_name: z.string().nullish(),
  bank_iban: z.string().nullish(),
  bank_swift_code: z.string().nullish(),
  shipping_address: z.string().nullish(),
  e_invoice: z.boolean().nullish(),
  ei_code: z.string().nullish(),
  discount_highlight: z.boolean().nullish(),
  default_discount: z.number().nullish(),
  has_intent_declaration: z.boolean().nullish(),
  intent_declaration_protocol_number: z.string().nullish(),
  intent_declaration_protocol_date: z.string().nullish(),
  created_at: z.string().nullish(),
  updated_at: z.string().nullish(),
});
