export type AccountMember = {
    id: number;
    name: string;
    email: string;
    role: string | null;
};

export type OrderField = {
    id: number;
    type: string;
    name: string;
    description: string | null;
    required: boolean;
    options: string[] | Record<string, unknown> | null;
};

export type AccountContext = {
    id: number;
    name: string;
    email: string;
    currency: string;
    country: string | null;
    tax_exempt: boolean;
    auto_apply_credit: boolean;
    credit_balance: number;
    vat_number: string | null;
    owner_user_id: number | null;
    members: AccountMember[];
    order_fields: OrderField[];
};

export type ProductHit = {
    id: number;
    name: string;
    type: string;
    visibility: string;
    group: string | null;
    out_of_stock: boolean;
    cycles: number[];
};

export type Cycle = {
    interval_months: number;
    setup: number;
    recurring: number;
    due_today: number;
    saving_percent?: number;
};

export type OptionValue = {
    id: number;
    label: string;
    slug: string;
    setup: number;
    recurring: number;
};

export type ProductOption = {
    id: number;
    name: string;
    slug: string;
    type: string;
    required: boolean;
    min_qty: number | null;
    max_qty: number | null;
    values: OptionValue[];
};

export type OptionGroup = {
    id: number;
    name: string;
    slug: string;
    options: ProductOption[];
};

export type ProductAddon = {
    id: number;
    name: string;
    required: boolean;
    preselected: boolean;
    interval_months: number;
    setup: number;
    recurring: number;
};

export type CustomField = {
    id: number;
    name: string;
    slug: string;
    type: string;
    required: boolean;
    options: string[] | Record<string, unknown> | null;
};

export type ProductConfigure = {
    id: number;
    name: string;
    type: string;
    cycles: Cycle[];
    default_interval: number | null;
    option_groups: OptionGroup[];
    addons: ProductAddon[];
    custom_fields: CustomField[];
    allow_qty: boolean;
    domain_mode: string;
    collect_hostname: boolean;
    collect_root_password: boolean;
    collect_nameservers: boolean;
    out_of_stock: boolean;
};

export type DomainCheck = {
    fqdn: string;
    tld_id: number;
    available: boolean;
    status: string;
    can_order: boolean;
    years: Array<{ years: number; amount: number }>;
    auth_code_required: boolean;
    privacy_allowed: boolean;
};

export type DomainDraft = {
    operation: 'register' | 'transfer' | 'own';
    fqdn: string;
    tld_id: number | null;
    years: number;
    auth_code: string;
    privacy: boolean;
    check: DomainCheck | null;
};

export type LineDraft = {
    key: string;
    kind: 'product' | 'domain';
    product_id: number | null;
    product_name: string;
    configure: ProductConfigure | null;
    interval_months: number;
    quantity: number;
    config: Record<string, string | boolean | number>;
    custom_fields: Record<string, string>;
    hostname: string;
    root_password: string;
    nameservers: string;
    addon_ids: number[];
    override_setup: string;
    override_recurring: string;
    override_amount: string;
    domain: DomainDraft;
};

export type QuoteLine = {
    kind: string;
    description: string;
    due_today: number;
    recurring: number;
    interval_months: number;
    overridden: boolean;
};

export type QuotePayload = {
    currency: string;
    lines: QuoteLine[];
    subtotal: number;
    discount: number;
    promo: { code: string; discount: number } | null;
    tax: number;
    total: number;
    recurring: Array<{ interval_months: number; amount: number }>;
    credit_balance: number;
    credit_applied: number;
    warnings: string[];
};

export function csrfHeaders(): Record<string, string> {
    const match = document.cookie.match(/XSRF-TOKEN=([^;]+)/);

    return {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'X-Requested-With': 'XMLHttpRequest',
        ...(match ? { 'X-XSRF-TOKEN': decodeURIComponent(match[1]) } : {}),
    };
}

export function intervalLabel(months: number): string {
    const labels: Record<number, string> = {
        1: 'Mensuel',
        3: 'Trimestriel',
        6: 'Semestriel',
        12: 'Annuel',
        24: 'Biennal',
        36: 'Triennal',
    };

    return labels[months] ?? `${months} mois`;
}

export function toMinor(value: string): number | null {
    const trimmed = value.trim().replace(/\s/g, '').replace(',', '.');

    if (trimmed === '') {
        return null;
    }

    const amount = Number(trimmed);

    if (!Number.isFinite(amount) || amount < 0) {
        return null;
    }

    return Math.round(amount * 100);
}
