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

// --- Interfaces ---

export interface CsvUploadItem {
    id: string;
    /** Etiqueta vía i18n: csv_upload.items.<id> */
    icon: any;
    /** Clave i18n csv_upload.groups.<group> */
    group?: string;
    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;
    /** Ruta bajo csv_upload.* (p. ej. params.load_mode) */
    labelKey: string;
    type: 'text' | 'select' | 'boolean' | 'date' | 'radio';
    options?: { labelKey: 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;
    labelKey: string;
    type: 'select';
    options: { labelKey: 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',
        labelKey: 'params.load_mode',
        type: 'select',
        options: [
            { labelKey: 'options.update', value: 'update' },
            { labelKey: 'options.replace', value: 'replace' }
        ],
        defaultValue: 'update'
    },
    {
        name: 'dateFormat',
        labelKey: 'params.date_format',
        type: 'select',
        options: [
            { labelKey: 'options.date_dd_mm', value: 'dd/mm/yyyy' },
            { labelKey: 'options.date_mm_dd', value: 'mm/dd/yyyy' },
            { labelKey: 'options.date_iso', value: 'yyyy-mm-dd' }
        ],
        defaultValue: 'dd/mm/yyyy'
    },
    {
        name: 'weight_unit',
        labelKey: 'params.weight_unit',
        type: 'select',
        options: [
            { labelKey: 'options.kgs', value: 'kgs' },
            { labelKey: 'options.lbs', value: 'lbs' }
        ],
        defaultValue: 'kgs'
    },
    {
        name: 'autonumber_series',
        labelKey: 'params.autonumber_series',
        type: 'select',
        options: [
            { labelKey: 'options.yes', value: 'true' },
            { labelKey: 'options.no', value: 'false' }
        ],
        defaultValue: 'false'
    },
    {
        name: 'load_subpartidas',
        labelKey: 'params.load_subpartidas',
        type: 'select',
        options: [
            { labelKey: 'options.yes', value: 'true' },
            { labelKey: 'options.no', value: 'false' }
        ],
        defaultValue: 'false'
    },
    {
        name: 'recalculate_pedimento_date',
        labelKey: 'params.recalculate_pedimento_date',
        type: 'select',
        options: [
            { labelKey: 'options.yes', value: 'true' },
            { labelKey: 'options.no', value: 'false' }
        ],
        defaultValue: 'false'
    }
];

// Map of Tab ID -> Array of Fields
export const tabSettings: Record<string, CsvUploadField[]> = {
    catalogos: [
        {
            name: 'mode',
            labelKey: 'params.load_mode',
            type: 'radio',
            options: [
                { labelKey: 'options.update', value: 'update' },
                { labelKey: 'options.replace', value: 'replace' }
            ],
            defaultValue: 'update'
        }
    ],
    transportes: [
        {
            name: 'mode',
            labelKey: 'params.load_mode',
            type: 'radio',
            options: [
                { labelKey: 'options.update', value: 'update' },
                { labelKey: 'options.replace', value: 'replace' }
            ],
            defaultValue: 'update'
        }
    ],
    importacion: [
        {
            name: 'autonumber_remesas',
            labelKey: 'params.autonumber_remesas',
            type: 'boolean',
            defaultValue: false
        },
        {
            name: 'recalculate_dates',
            labelKey: 'params.recalculate_dates',
            type: 'boolean',
            defaultValue: false
        },
        {
            name: 'dateFormat',
            labelKey: 'params.date_format',
            type: 'select',
            options: [
                { labelKey: 'options.date_dd_mm', value: 'dd/mm/yyyy' },
                { labelKey: 'options.date_mm_dd', value: 'mm/dd/yyyy' },
                { labelKey: 'options.date_iso', value: 'yyyy-mm-dd' }
            ],
            defaultValue: 'dd/mm/yyyy'
        }
    ],
    exportacion: [
        {
            name: 'invoice_type',
            labelKey: 'params.invoice_type',
            type: 'select',
            options: [
                { labelKey: 'options.afi', value: 'AFIJO' },
                { labelKey: 'options.normal', value: 'NORMAL' },
            ],
            defaultValue: 'AFIJO',
        },
        {
            name: 'is_regime_change',
            labelKey: 'params.is_regime_change',
            type: 'boolean',
            defaultValue: false,
        },
        {
            name: 'dateFormat',
            labelKey: 'params.date_format',
            type: 'select',
            options: [
                { labelKey: 'options.date_dd_mm', value: 'dd/mm/yyyy' },
                { labelKey: 'options.date_mm_dd', value: 'mm/dd/yyyy' },
                { labelKey: 'options.date_iso', 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',
        icon: User,
        modelTarget: 'CustomsBroker',
        templateId: 'customs_brokers',
        layoutModule: 'layouts_csv/customs_brokers'
    },
    {
        id: 'clients_providers',
        icon: Users,
        modelTarget: 'ClientProvider',
        templateId: 'clients_providers',
        layoutModule: 'layouts_csv/clients_and_providers'
    },
    {
        id: 'exchange_rates',
        icon: DollarSign,
        modelTarget: 'ExchangeRate',
        templateId: 'exchange_rates',
        layoutModule: 'layouts_csv/exchange_rate'
    },
    {
        id: 'material_classes',
        icon: Package,
        modelTarget: 'MaterialClass',
        templateId: 'material_classes',
        layoutModule: 'layouts_csv/classes'
    },
    {
        id: 'part_numbers',
        icon: Hash,
        modelTarget: 'Part',
        templateId: 'part_numbers',
        layoutModule: 'layouts_csv/parts'
    },
    {
        id: 'boms',
        icon: Briefcase,
        modelTarget: 'Bom',
        templateId: 'boms',
        layoutModule: 'layouts_csv/boms',
        disabled: true
    },
    {
        id: 'items',
        icon: FileText,
        group: 'permisos',
        modelTarget: 'ItemPermission',
        templateId: 'part_numbers'
    },
    {
        id: 'headers',
        icon: FileText,
        group: 'permisos',
        modelTarget: 'HeaderPermission',
        disabled: true,
    },
    {
        id: 'historical_fractions',
        icon: Calendar,
        modelTarget: 'HistoricalFraction',
        disabled: true,
    },
    {
        id: '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',
        icon: Ship,
        modelTarget: 'Transporter',
        templateId: 'transporters',
        layoutModule: 'layouts_csv/transportistas'
    },
    {
        id: 'transports',
        icon: Truck,
        modelTarget: 'Transport',
        templateId: 'transports',
        layoutModule: 'layouts_csv/vehicles'
    },
    {
        id: 'drivers',
        icon: User,
        modelTarget: 'Driver',
        templateId: 'drivers',
        layoutModule: 'layouts_csv/drivers'
    },
    {
        id: 'trailers',
        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',
        icon: FileText,
        group: 'impo_temp',
        modelTarget: 'invoice_header',
        templateId: 'imp_temp_header',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_temp_details',
        icon: Package,
        group: 'impo_temp',
        modelTarget: 'invoice_details',
        templateId: 'imp_temp_details',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_temp_series',
        icon: Hash,
        group: 'impo_temp',
        modelTarget: 'invoice_series',
        templateId: 'imp_temp_series',
        layoutModule: 'layouts_csv/facturas'
    },
    // Impo Def
    {
        id: 'imp_def_header',
        icon: FileText,
        group: 'impo_def',
        modelTarget: 'invoice_header',
        templateId: 'imp_def_header',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_def_details',
        icon: Package,
        group: 'impo_def',
        modelTarget: 'invoice_details',
        templateId: 'imp_def_details',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'imp_def_series',
        icon: Hash,
        group: 'impo_def',
        modelTarget: 'invoice_series',
        templateId: 'imp_def_series',
        layoutModule: 'layouts_csv/facturas'
    },
    // Compras Mex
    {
        id: 'comp_mex_header',
        icon: FileText,
        group: 'cmex',
        modelTarget: 'invoice_header',
        templateId: 'cmex_header',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'comp_mex_details',
        icon: Package,
        group: 'cmex',
        modelTarget: 'invoice_details',
        templateId: 'cmex_details',
        layoutModule: 'layouts_csv/facturas'
    },
    {
        id: 'comp_mex_series',
        icon: Hash,
        group: 'cmex',
        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',
        icon: FileText,
        group: 'expo_def',
        modelTarget: 'invoice_header',
        templateId: 'exp_def_header',
        layoutModule: 'layouts_csv/exportacion'
    },
    {
        id: 'exp_def_details',
        icon: Package,
        group: 'expo_def',
        modelTarget: 'invoice_details',
        templateId: 'exp_def_details',
        layoutModule: 'layouts_csv/exportacion'
    },
    {
        id: 'exp_def_series',
        icon: Hash,
        group: 'expo_def',
        modelTarget: 'invoice_series',
        templateId: 'exp_def_series',
        layoutModule: 'layouts_csv/exportacion'
    },
    {
        id: 'exp_def_nodes',
        icon: Briefcase,
        group: 'expo_def',
        modelTarget: 'Nodes',
        disabled: true,
    },
    // Expo Rep
    {
        id: 'exp_rep_header',
        icon: FileText,
        group: 'expo_rep',
        modelTarget: 'InvoiceHeader',
        disabled: true,
    },
    {
        id: 'exp_rep_details',
        icon: Package,
        group: 'expo_rep',
        modelTarget: 'InvoiceSalesDetails',
        disabled: true,
    },
    {
        id: 'exp_rep_series',
        icon: Hash,
        group: 'expo_rep',
        modelTarget: 'InvoiceSeries',
        disabled: true,
    },
    // Manifiesto
    {
        id: 'manifest_header',
        icon: FileText,
        group: 'manifest',
        modelTarget: 'Manifest',
        disabled: true,
    },
];
