import { z } from "zod";
import {
  nonNegativeNumberFromForm,
  numberInRange,
  emailOrEmpty,
} from "./common";

export const UpdateCompanySettingsSchema = z.object({
  company_name: z.string().nullish(),
  street: z.string().nullish(),
  city: z.string().nullish(),
  postal_code: z.string().nullish(),
  country: z.string().nullish(),
  company_id: z.string().nullish(),
  vat_id: z.string().nullish(),
  quotation_prefix: z.string().nullish(),
  default_currency: z.string().nullish(),
  order_type_code: z.string().nullish(),
  invoice_type_code: z.string().nullish(),
  default_vat_rate: numberInRange(0, 100).optional(),
  require_2fa: z
    .preprocess((v) => v === true || v === 1 || v === "1", z.boolean())
    .optional(),
  break_threshold_hours: nonNegativeNumberFromForm.optional(),
  break_duration_short: nonNegativeNumberFromForm.optional(),
  break_duration_long: nonNegativeNumberFromForm.optional(),
  clock_rounding_minutes: numberInRange(0, 1440).optional(),
  invoice_alert_email: emailOrEmpty.nullish(),
  leave_notify_email: emailOrEmpty.nullish(),
  smtp_from: emailOrEmpty.nullish(),
  smtp_from_name: z.string().nullish(),
  offer_number_pattern: z.string().nullish(),
  order_number_pattern: z.string().nullish(),
  invoice_number_pattern: z.string().nullish(),
  issued_order_number_pattern: z.string().nullish(),
  issued_order_type_code: z.string().nullish(),
  project_number_pattern: z.string().nullish(),
  project_type_code: z.string().nullish(),
  warehouse_receipt_prefix: z.string().nullish(),
  warehouse_receipt_number_pattern: z.string().nullish(),
  warehouse_issue_prefix: z.string().nullish(),
  warehouse_issue_number_pattern: z.string().nullish(),
  warehouse_inventory_prefix: z.string().nullish(),
  warehouse_inventory_number_pattern: z.string().nullish(),
  max_login_attempts: numberInRange(1, 100).optional(),
  lockout_minutes: numberInRange(1, 1440).optional(),
  max_requests_per_minute: numberInRange(1, 100000).optional(),
  available_vat_rates: z.array(numberInRange(0, 100)).max(50).optional(),
  available_currencies: z.array(z.string()).optional(),
  custom_fields: z.array(z.unknown()).max(200).optional(),
  supplier_field_order: z.array(z.unknown()).max(200).optional(),
});

export type UpdateCompanySettingsInput = z.infer<
  typeof UpdateCompanySettingsSchema
>;
