import {
    User,
    Users,
    FileText,
    Truck,
    Container,
    Ship,
    Plane,
    Package,
    Briefcase,
    Globe,
    CreditCard,
    DollarSign,
    Calendar,
    Hash,
    MapPin,
    ShieldCheck,
    FileDigit,
    Scale,
} from 'lucide-svelte';

// --- Interfaces ---

export interface CsvUploadItem {
    id: string;
    title: string;
    icon: any;
    group?: string; // For grouping within a tab
    modelTarget?: string; // The backend model this maps to
    description?: string;
    /** Backend template id for CSV download (e.g. customs_brokers, part_numbers). No physical file. */
    templateId?: string;
    disabled?: boolean; // New property to mark items as "Coming Soon"
    /** Backend module path (layouts_csv) for traceability, e.g. "layouts_csv/facturas", "layouts_csv/parts". */
    layoutModule?: string;
}

export interface CsvUploadField {
    name: string;
    label: string;
    type: 'text' | 'select' | 'boolean' | 'date' | 'radio';
    options?: { label: string; value: string | boolean | number }[];
    required?: boolean;
    defaultValue?: any;
}

/** Global parameters shown in the CSV upload footer bar (one option or the other via select). */
export interface GlobalCsvParam {
    name: string;
    label: string;
    type: 'select';
    options: { label: string; value: string }[];
    defaultValue: string;
}

/** Global parameters for all CSV loads; merged with tab-specific settings when sending footer_config. */
export const globalCsvParams: GlobalCsvParam[] = [
    {
        name: 'mode',
        label: 'Modo de Carga',
        type: 'select',
        options: [
            { label: 'Actualizar', value: 'update' },
            { label: 'Reemplazar', value: 'replace' }
        ],
        defaultValue: 'update'
    },
    {
        name: 'dateFormat',
        label: 'Formato de Fecha',
        type: 'select',
        options: [
            { label: 'DD/MM/YYYY', value: 'dd/mm/yyyy' },
            { label: 'MM/DD/YYYY', value: 'mm/dd/yyyy' },
            { label: 'YYYY-MM-DD', value: 'yyyy-mm-dd' }
        ],
        defaultValue: 'dd/mm/yyyy'
    },
    {
        name: 'weight_unit',
        label: 'Unidad de Peso',
        type: 'select',
        options: [
            { label: 'Kilos (Kgs)', value: 'kgs' },
            { label: 'Libras (Lbs)', value: 'lbs' }
        ],
        defaultValue: 'kgs'
    },
    {
        name: 'autonumber_series',
        label: 'Autonumerar Partidas/Series',
        type: 'select',
        options: [
            { label: 'Sí', value: 'true' },
            { label: 'No', value: 'false' }
        ],
        defaultValue: 'false'
    },
    {
        name: 'load_subpartidas',
        label: 'Levantar Subpartidas',
        type: 'select',
        options: [
            { label: 'Sí', value: 'true' },
            { label: 'No', value: 'false' }
        ],
        defaultValue: 'false'
    },
    {
        name: 'recalculate_pedimento_date',
        label: 'Recalcular Fecha Pedimento',
        type: 'select',
        options: [
            { label: 'Sí', value: 'true' },
            { label: 'No', value: 'false' }
        ],
        defaultValue: 'false'
    }
];

// Map of Tab ID -> Array of Fields
export const tabSettings: Record<string, CsvUploadField[]> = {
    catalogos: [
        {
            name: 'mode',
            label: 'Modo de Carga',
            type: 'radio',
            options: [
                { label: 'Actualizar', value: 'update' },
                { label: 'Reemplazar', value: 'replace' }
            ],
            defaultValue: 'update'
        }
    ],
    transportes: [
        {
            name: 'mode',
            label: 'Modo de Carga',
            type: 'radio',
            options: [
                { label: 'Actualizar', value: 'update' },
                { label: 'Reemplazar', value: 'replace' }
            ],
            defaultValue: 'update'
        }
    ],
    importacion: [
        {
            name: 'autonumber_remesas',
            label: 'Autonumerar Remesas',
            type: 'boolean',
            defaultValue: false
        },
        {
            name: 'recalculate_dates',
            label: 'Recalcular Fechas',
            type: 'boolean',
            defaultValue: false
        },
        {
            name: 'dateFormat',
            label: 'Formato de Fecha',
            type: 'select',
            options: [
                { label: 'DD/MM/YYYY', value: 'dd/mm/yyyy' },
                { label: 'MM/DD/YYYY', value: 'mm/dd/yyyy' },
                { label: 'YYYY-MM-DD', value: 'yyyy-mm-dd' }
            ],
            defaultValue: 'dd/mm/yyyy'
        }
    ],
    exportacion: [
        {
            name: 'invoice_type',
            label: 'Tipo de Factura',
            type: 'select',
            options: [
                { label: 'AFIJO', value: 'AFIJO' },
                { label: 'NORMAL', value: 'NORMAL' },
            ],
            defaultValue: 'AFIJO',
        },
        {
            name: 'is_regime_change',
            label: 'Es Cambio de Régimen',
            type: 'boolean',
            defaultValue: false,
        },
        {
            name: 'dateFormat',
            label: 'Formato de Fecha',
            type: 'select',
            options: [
                { label: 'DD/MM/YYYY', value: 'dd/mm/yyyy' },
                { label: 'MM/DD/YYYY', value: 'mm/dd/yyyy' },
                { label: 'YYYY-MM-DD', value: 'yyyy-mm-dd' }
            ],
            defaultValue: 'dd/mm/yyyy'
        }
    ]
};

// --- DATA DEFINITIONS (Items only, no config) ---
// Catálogos: la mayoría usa backend en layouts_csv (customs_brokers, clients_and_providers, etc.).
export const catalogosConfig: CsvUploadItem[] = [
    {
        id: 'customs_brokers',
        title: 'Agentes Aduanales',
        icon: User,
        modelTarget: 'CustomsBroker',
        templateId: 'customs_brokers',
        layoutModule: 'layouts_csv/customs_brokers'
    },
    {
        id: 'clients_providers',
        title: 'Clientes y Proveedores',
        icon: Users,
        modelTarget: 'ClientProvider',
        templateId: 'clients_providers',
        layoutModule: 'layouts_csv/clients_and_providers'
    },
    {
        id: 'exchange_rates',
        title: 'Tipo de Cambios',
        icon: DollarSign,
        modelTarget: 'ExchangeRate',
        templateId: 'exchange_rates',
        layoutModule: 'layouts_csv/exchange_rate'
    },
    {
        id: 'american_fractions',
        title: 'Fracc. Ame.',
        icon: Globe,
        modelTarget: 'AmericanFraction',
        templateId: 'american_fractions',
        layoutModule: 'layouts_csv/us_tariff_fractions'
    },
    {
        id: 'material_classes',
        title: 'Clases de Materiales',
        icon: Package,
        modelTarget: 'MaterialClass',
        templateId: 'material_classes',
        layoutModule: 'layouts_csv/classes'
    },
    {
        id: 'part_numbers',
        title: 'Números de parte',
        icon: Hash,
        modelTarget: 'Part',
        templateId: 'part_numbers',
        layoutModule: 'layouts_csv/parts'
    },
    {
        id: 'boms',
        title: 'BOMs',
        icon: Briefcase,
        modelTarget: 'Bom',
        templateId: 'boms',
        layoutModule: 'layouts_csv/boms'
    },
    {
        id: 'items',
        title: 'Partidas (Permisos)',
        icon: FileText,
        group: 'Permisos',
        modelTarget: 'ItemPermission',
        templateId: 'part_numbers'
    },
    {
        id: 'headers',
        title: 'Encabezados (Permisos)',
        icon: FileText,
        group: 'Permisos',
        modelTarget: 'HeaderPermission',
        disabled: true,
    },
    {
        id: 'historical_fractions',
        title: 'Fracciones Históricas',
        icon: Calendar,
        modelTarget: 'HistoricalFraction',
        disabled: true,
    },
    {
        id: 'pedimentos',
        title: 'Pedimentos',
        icon: FileDigit,
        modelTarget: 'Pedimento',
        templateId: 'pedimentos',
        layoutModule: 'layouts_csv/pedmientos'
    },
];

// Transportes: conductores, trailers y “Transportes” (vehículos) usan layouts_csv; transportistas sin layouts_csv aún.
export const transportesConfig: CsvUploadItem[] = [
    {
        id: 'transporters',
        title: 'Transportistas',
        icon: Ship,
        modelTarget: 'Transporter',
        templateId: 'transporters',
        layoutModule: 'layouts_csv/transportistas'
    },
    {
        id: 'transports',
        title: 'Transportes',
        icon: Truck,
        modelTarget: 'Transport',
        templateId: 'transports',
        layoutModule: 'layouts_csv/vehicles'
    },
    {
        id: 'drivers',
        title: 'Conductores',
        icon: User,
        modelTarget: 'Driver',
        templateId: 'drivers',
        layoutModule: 'layouts_csv/drivers'
    },
    {
        id: 'trailers',
        title: 'Trailers y Cajas',
        icon: Container,
        modelTarget: 'Trailer',
        templateId: 'trailers',
        layoutModule: 'layouts_csv/trailers'
    },
];

// --- Operaciones de Importación (facturas: encabezados y partidas)
// Backend: layouts_csv/facturas — rutas /v1/a76/imports/ (upload, status, commit).
export const importacionConfig: CsvUploadItem[] = [
    // Impo Temp
    {
        id: 'imp_temp_header',
        title: 'Encabezado',
        icon: FileText,
        group: 'Impo. Temp.',
        modelTarget: 'invoice_header',
        templateId: 'imp_temp_header',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_temp_details',
        title: 'Partidas',
        icon: Package,
        group: 'Impo. Temp.',
        modelTarget: 'invoice_details',
        templateId: 'imp_temp_details',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_temp_series',
        title: 'Series',
        icon: Hash,
        group: 'Impo. Temp.',
        modelTarget: 'invoice_series',
        templateId: 'imp_temp_series',
        layoutModule: 'layouts_csv/facturas'
    },
    // Impo Def
    {
        id: 'imp_def_header',
        title: 'Encabezado',
        icon: FileText,
        group: 'Impo. Def.',
        modelTarget: 'invoice_header',
        templateId: 'imp_def_header',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_def_details',
        title: 'Partidas',
        icon: Package,
        group: 'Impo. Def.',
        modelTarget: 'invoice_details',
        templateId: 'imp_def_details',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_def_series',
        title: 'Series',
        icon: Hash,
        group: 'Impo. Def.',
        modelTarget: 'invoice_series',
        templateId: 'imp_def_series',
        layoutModule: 'layouts_csv/facturas'
    },
    // Compras Mex
    {
        id: 'comp_mex_header',
        title: 'Encabezado',
        icon: FileText,
        group: 'Compras Mex.',
        modelTarget: 'invoice_header',
        templateId: 'cmex_header',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'comp_mex_details',
        title: 'Partidas',
        icon: Package,
        group: 'Compras Mex.',
        modelTarget: 'invoice_details',
        templateId: 'cmex_details',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'comp_mex_series',
        title: 'Series',
        icon: Hash,
        group: 'Compras Mex.',
        modelTarget: 'invoice_series',
        templateId: 'cmex_series',
        layoutModule: 'layouts_csv/facturas'
    },
];

// --- Operaciones de Exportación (facturas: encabezados y partidas)
// Backend: layouts_csv/exportacion — rutas /v1/a76/imports/exportacion/
export const exportacionConfig: CsvUploadItem[] = [
    // Expo Def / Cam. Reg.
    {
        id: 'exp_def_header',
        title: 'Encabezado',
        icon: FileText,
        group: 'Expo. Def./Cam. Reg.',
        modelTarget: 'invoice_header',
        templateId: 'exp_def_header',
        layoutModule: 'layouts_csv/exportacion'
    },
    {
        id: 'exp_def_details',
        title: 'Partidas',
        icon: Package,
        group: 'Expo. Def./Cam. Reg.',
        modelTarget: 'invoice_details',
        templateId: 'exp_def_details',
        layoutModule: 'layouts_csv/exportacion'
    },
    {
        id: 'exp_def_series',
        title: 'Series',
        icon: Hash,
        group: 'Expo. Def./Cam. Reg.',
        modelTarget: 'invoice_series',
        templateId: 'exp_def_series',
        layoutModule: 'layouts_csv/exportacion'
    },
    {
        id: 'exp_def_nodes',
        title: 'NODES',
        icon: Briefcase,
        group: 'Expo. Def./Cam. Reg.',
        modelTarget: 'Nodes',
        disabled: true,
    },
    // Expo Rep
    {
        id: 'exp_rep_header',
        title: 'Encabezado',
        icon: FileText,
        group: 'Expo. Rep.',
        modelTarget: 'InvoiceHeader',
        disabled: true,
    },
    {
        id: 'exp_rep_details',
        title: 'Partidas',
        icon: Package,
        group: 'Expo. Rep.',
        modelTarget: 'InvoiceSalesDetails',
        disabled: true,
    },
    {
        id: 'exp_rep_series',
        title: 'Series',
        icon: Hash,
        group: 'Expo. Rep.',
        modelTarget: 'InvoiceSeries',
        disabled: true,
    },
    // Manifiesto
    {
        id: 'manifest_header',
        title: 'Encabezado',
        icon: FileText,
        group: 'Manifiesto',
        modelTarget: 'Manifest',
        disabled: true,
    },
];
