import { z } from "zod";

import { createApiResponseSchema } from "./Factory";

const nullableStringArraySchema = z
  .array(z.string())
  .nullish()
  .transform((value) => value ?? []);

const nullableArraySchema = z
  .array(z.any())
  .nullish()
  .transform((value) => value ?? []);
export interface IAddress {
  street: string;
  city: string;
  state: string;
  postal_code: string;
  country: string;
}

export interface IContactPerson {
  full_name: string;
  first_name: string;
  last_name: string;
  email: string;
  phone: string;
  mobile: string;
  position: string;
  is_primary: boolean;
}

export interface ITags {
  keywords: string[];
  categories: any[];
  specializations: string[];
  certifications: string[];
  cpv_codes: string[];
}

export interface ICreateCompanyCredentials {
  name: string;
  email: string;
  phone: string;
  website: string;
  type: string;
  description: string;
  founded_year?: number | string;
  employee_count?: number | string;
  annual_revenue?: number | string;
  currency: string;
  industry: string;
  language: string;
  tax_id: string;
  registration_number: string;
  timezone: string;
  address: IAddress;
  contact_person?: IContactPerson;
  tags: ITags;
  document_file_ids?: string[];
}

export const companySchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().optional(),
  phone: z.string().optional(),
  website: z.string(),
  type: z.string(),
  status: z.string().optional(),
  description: z.string(),
  founded_year: z.number().nullish(),
  employee_count: z.number().nullish(),
  annual_revenue: z.number().nullish(),
  currency: z.string(),
  industry: z.string(),
  language: z.string(),
  tax_id: z.string(),
  registration_number: z.string(),
  timezone: z.string(),
  is_verified: z.boolean().optional(),
  is_compliant: z.boolean().optional(),
  created_at: z.number().optional(),
  updated_at: z.number().optional(),
  address: z.object({
    street: z.string(),
    city: z.string(),
    state: z.string(),
    postal_code: z.string(),
    country: z.string(),
  }),
  contact_person: z
    .object({
      full_name: z.string(),
      first_name: z.string(),
      last_name: z.string(),
      email: z.string().email(),
      phone: z.string(),
      mobile: z.string(),
      position: z.string(),
      is_primary: z.boolean(),
    })
    .nullable()
    .optional(),
  tags: z.object({
    keywords: nullableStringArraySchema,
    categories: nullableArraySchema,
    specializations: nullableStringArraySchema,
    certifications: nullableStringArraySchema,
    cpv_codes: nullableStringArraySchema,
  }),
  document_file_ids: z.array(z.string()).or(z.null()).optional(),
});

export const companiesResponseSchema = z.object({
  companies: z.array(companySchema).or(z.null()),
});

export const companyDetailsApiResponseSchema =
  createApiResponseSchema(companySchema);
export const CompaniesListResponseSchema = createApiResponseSchema(
  companiesResponseSchema,
);
export type TCompany = z.infer<typeof companySchema>;
export type TCompaniesResponse = z.infer<typeof companiesResponseSchema>;
