import { z } from "zod";

// ============================================================================
// Request Parameter Types
// ============================================================================

/**
 * Parameters for WHMCS ValidateLogin API
 */
export interface WhmcsValidateLoginParams {
  email: string;
  password2: string;
  [key: string]: unknown;
}

/**
 * Parameters for WHMCS AddClient API
 */
export interface WhmcsAddClientParams {
  firstname: string;
  lastname: string;
  email: string;
  address1?: string;
  city?: string;
  state?: string;
  postcode?: string;
  country?: string;
  phonenumber?: string;
  password2: string;
  companyname?: string;
  currency?: string;
  groupid?: number;
  customfields?: Record<string, string>;
  language?: string;
  clientip?: string;
  notes?: string;
  marketing_emails_opt_in?: boolean;
  no_email?: boolean;
  [key: string]: unknown;
}

/**
 * Parameters for WHMCS CreateSsoToken API
 */
export interface WhmcsCreateSsoTokenParams {
  client_id: number;
  destination?: string;
  sso_redirect_path?: string;
  [key: string]: unknown;
}

// ============================================================================
// Response Types
// ============================================================================

const booleanLike = z.union([z.boolean(), z.number(), z.string()]);
const numberLike = z.union([z.number(), z.string()]);

export const whmcsCustomFieldSchema = z
  .object({
    id: numberLike,
    value: z.string().optional().nullable(),
    name: z.string().optional(),
    type: z.string().optional(),
  })
  .passthrough();

export const whmcsUserSchema = z
  .object({
    id: numberLike,
    name: z.string(),
    email: z.string(),
    is_owner: booleanLike.optional(),
  })
  .passthrough();

export const whmcsEmailPreferencesSchema = z
  .record(z.string(), z.union([z.string(), z.number(), z.boolean()]))
  .optional();

const customFieldsSchema = z
  .union([
    z.array(whmcsCustomFieldSchema),
    z
      .object({
        customfield: z.union([whmcsCustomFieldSchema, z.array(whmcsCustomFieldSchema)]),
      })
      .passthrough(),
  ])
  .optional();

const usersSchema = z
  .object({ user: z.union([whmcsUserSchema, z.array(whmcsUserSchema)]) })
  .passthrough()
  .optional();

export const whmcsClientSchema = z
  .object({
    client_id: numberLike.optional(),
    owner_user_id: numberLike.optional(),
    userid: numberLike.optional(),
    id: numberLike,
    uuid: z.string().optional(),
    firstname: z.string().optional(),
    lastname: z.string().optional(),
    fullname: z.string().optional(),
    companyname: z.string().optional(),
    email: z.string(),
    address1: z.string().optional(),
    address2: z.string().optional(),
    city: z.string().optional(),
    state: z.string().optional(),
    fullstate: z.string().optional(),
    statecode: z.string().optional(),
    postcode: z.string().optional(),
    country: z.string().optional(),
    countrycode: z.string().optional(),
    phonecc: z.string().optional(),
    phonenumber: z.string().optional(),
    phonenumberformatted: z.string().optional(),
    telephoneNumber: z.string().optional(),
    tax_id: z.string().optional(),
    currency: numberLike.optional(),
    currency_code: z.string().optional(),
    defaultgateway: z.string().optional(),
    defaultpaymethodid: numberLike.optional(),
    language: z.string().optional(),
    status: z.string().optional(),
    notes: z.string().optional(),
    datecreated: z.string().optional(),
    lastlogin: z.string().optional(),
    email_preferences: whmcsEmailPreferencesSchema,
    allowSingleSignOn: booleanLike.optional(),
    email_verified: booleanLike.optional(),
    marketing_emails_opt_in: booleanLike.optional(),
    isOptedInToMarketingEmails: booleanLike.optional(),
    phoneNumber: z.string().optional(),
    customfields: customFieldsSchema,
    users: usersSchema,
  })
  .catchall(z.unknown());

export const whmcsClientStatsSchema = z
  .record(z.string(), z.union([z.string(), z.number(), z.boolean()]))
  .optional();

export const whmcsClientResponseSchema = z
  .object({
    result: z.string().optional(),
    client_id: numberLike.optional(),
    client: whmcsClientSchema,
    stats: whmcsClientStatsSchema,
  })
  .catchall(z.unknown());

export type WhmcsCustomField = z.infer<typeof whmcsCustomFieldSchema>;
export type WhmcsUser = z.infer<typeof whmcsUserSchema>;
export type WhmcsClient = z.infer<typeof whmcsClientSchema>;
export type WhmcsClientResponse = z.infer<typeof whmcsClientResponseSchema>;

// ============================================================================
// WHMCS Add Client Response
// ============================================================================

/**
 * WHMCS AddClient API response schema
 */
export const whmcsAddClientResponseSchema = z.object({
  clientid: z.number(),
});

export type WhmcsAddClientResponse = z.infer<typeof whmcsAddClientResponseSchema>;

// ============================================================================
// WHMCS Validate Login Response
// ============================================================================

/**
 * WHMCS ValidateLogin API response schema
 */
export const whmcsValidateLoginResponseSchema = z.object({
  userid: z.number(),
  passwordhash: z.string(),
  pwresetkey: z.string().optional(),
});

export type WhmcsValidateLoginResponse = z.infer<typeof whmcsValidateLoginResponseSchema>;

// ============================================================================
// WHMCS SSO Response
// ============================================================================

/**
 * WHMCS CreateSsoToken API response schema
 */
export const whmcsSsoResponseSchema = z.object({
  redirect_url: z.string(),
});

export type WhmcsSsoResponse = z.infer<typeof whmcsSsoResponseSchema>;
export type WhmcsClientStats = z.infer<typeof whmcsClientStatsSchema>;
