-- Valores por defecto de pantallas / módulos por empresa (bootstrap SQL-first).
-- Orden esperado del seeder: carriers.sql + packtypes.sql antes de este archivo.
-- configpickup.shippingtype_id apunta a la tabla global `shippingtype` (debe existir al menos una fila).
-- BL/HBL no tienen tabla propia de configuración UI en esta capa (usan guías/consolidados).
--
INSERT INTO configprepack (maincompany_id, agency, weight, picture, aux, labelaux, type, date, emailtoagency)
SELECT :maincompany_id, 0, 0, 0, 0, NULL, 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM configprepack cp WHERE cp.maincompany_id = :maincompany_id
);

INSERT INTO configguide (
    maincompany_id, masterguide, pa, labelcontent, convertion, cod, commission, newstatusclosed, labelmaster, showtracking, tracking, showtype,
    value, usebillto, billto, auxdate, labelauxdate, showtransferable, transferable, assignuser, paidtype, updateinconsol, aux, requiredaux,
    requireagency, requireagent, labelaux, defaultlocation, auxdate2, labelauxdate2, requiredauxdate2, downpayment, labeldownpayment,
    google_address, avoid_highways, avoid_tolls, unitdistance, statuspiece, downpayment2, finish_all_packages, labeldownpayment2, packlabel,
    sendshipper, sendreceiver, whatsappshipper, whatsappreceiver, hashazard
)
SELECT
    :maincompany_id, 0, 0, 'Contenido', 0, 0, 0, 0, 'Guía Master', 0, 0, 0,
    0, 0, 'shipper', 0, 'Fecha Auxiliar', 0, 0, 0, 0, 0, 0, 0,
    0, 0, NULL, 'none', 0, 'Fecha Auxiliar', 0, 0, 'Pago Inicial',
    0, 0, 0, 'NINGUNA', 0, 0, 0, 'Anticipo', 0,
    1, 1, 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM configguide cg WHERE cg.maincompany_id = :maincompany_id
);

INSERT INTO configawb (maincompany_id, weightunit)
SELECT :maincompany_id, 'Kg'
WHERE NOT EXISTS (
    SELECT 1 FROM configawb ca WHERE ca.maincompany_id = :maincompany_id
);

INSERT INTO configwhrec (
    maincompany_id,
    chargevolume,
    chargevalue,
    sendshipper,
    sendreceiver,
    onlybasic,
    creationdate,
    cod,
    tracking,
    value,
    instruction,
    consoltype_id,
    agencynote_id,
    tobill,
    labelnote,
    poboxedit,
    templateservices,
    aux,
    requiredaux,
    labelaux,
    requiredshipper,
    requiredreceiver,
    defaultlocation,
    packlabel,
    copyemail,
    copyto,
    whatsappshipper,
    whatsappreceiver,
    reempackconsoltype,
    agentfastwhrec,
    descriptionfastwhrec,
    cleanagencyfastwhrec,
    entertracking,
    show_shipping_type,
    show_measures,
    show_carrier,
    show_package_type,
    fastwhreclabel
)
SELECT
    :maincompany_id,
    0,
    '500',
    1,
    1,
    0,
    0,
    0,
    0,
    0,
    'Ninguna',
    NULL,
    NULL,
    0,
    'Nota',
    0,
    0,
    0,
    0,
    'Auxiliar',
    1,
    1,
    'Ninguna',
    0,
    0,
    NULL,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM configwhrec cw WHERE cw.maincompany_id = :maincompany_id
);

INSERT INTO `configreceipt` (
    maincompany_id,
    carrier_id,
    epacktype_id,
    shippingtype,
    tracking,
    value,
    packtype,
    labelnote,
    poboxedit,
    unitweight,
    unitvolume,
    ownweight,
    ownvolume,
    agentfastpack,
    requiredshipper,
    requiredreceiver,
    defaultlocation,
    entertracking,
    descriptionfastpack,
    cleanagencyfastpack,
    requiredlocation,
    note,
    description,
    getvalue,
    usevalue,
    weight,
    fastpacklabel,
    basicposition,
    packlabel,
    show_shipping_type,
    show_measures,
    show_carrier,
    show_package_type,
    processwithalert,
    dimension_unit,
    sendshipper,
    sendreceiver,
    whatsappshipper,
    whatsappreceiver,
    reempackconsoltype
)
SELECT
    :maincompany_id,
    (SELECT id FROM carrier c WHERE c.maincompany_id = :maincompany_id AND c.name = 'Aéreo' ORDER BY c.id ASC LIMIT 1),
    (SELECT id FROM packtype p WHERE p.maincompany_id = :maincompany_id AND p.name = 'Caja' ORDER BY p.id ASC LIMIT 1),
    0,
    0,
    0,
    'Caja',
    'Nota',
    0,
    0,
    0,
    0,
    0,
    0,
    1,
    1,
    'Ninguna',
    1,
    0,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    0,
    'END',
    0,
    0,
    0,
    0,
    0,
    1,
    0,
    1,
    1,
    0,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM `configreceipt` cr WHERE cr.maincompany_id = :maincompany_id
);

INSERT INTO `configocrdeployment` (
    maincompany_id,
    require_shipping_type,
    require_carrier_match,
    use_agency_as_shipper,
    customer_match_log_enabled
)
SELECT :maincompany_id, 0, 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM `configocrdeployment` co WHERE co.maincompany_id = :maincompany_id
);

-- Alertas (carrier por defecto = primer carrier "Aéreo" de la empresa)
INSERT INTO configalert (
    maincompany_id,
    arrivedate,
    aux,
    document,
    labelaux,
    labelimage,
    labeldocument,
    labeldescription,
    description,
    requiredaux,
    weight,
    value,
    pieces,
    carrier_id
)
SELECT
    :maincompany_id,
    1,
    0,
    0,
    NULL,
    'Imagen',
    NULL,
    'Descripción',
    1,
    0,
    '0.00',
    '0.00',
    1,
    (SELECT id FROM carrier c WHERE c.maincompany_id = :maincompany_id AND c.name = 'Aéreo' ORDER BY c.id ASC LIMIT 1)
WHERE NOT EXISTS (
    SELECT 1 FROM configalert ca WHERE ca.maincompany_id = :maincompany_id
);

-- Consolidados
INSERT INTO configconsol (
    maincompany_id,
    aux,
    labelaux,
    requiredaux,
    auxdate,
    labelauxdate,
    requiredauxdate,
    aux2,
    labelaux2,
    requiredaux2,
    creationdate,
    city,
    labelcity,
    requiredcity,
    supplier,
    samesr,
    accceptempty,
    editnoempty,
    generatezip
)
SELECT
    :maincompany_id,
    0,
    NULL,
    0,
    0,
    NULL,
    0,
    0,
    'Auxiliar 2',
    0,
    0,
    0,
    'Ciudad',
    0,
    0,
    0,
    0,
    0,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM configconsol cc WHERE cc.maincompany_id = :maincompany_id
);

-- Pagos
INSERT INTO `configpayment` (
    maincompany_id,
    document,
    labeldocument,
    requireddocument,
    extraaudit,
    labelextraaudit,
    requireextraaudit,
    labelnote,
    unrepeat_reference
)
SELECT
    :maincompany_id,
    0,
    'Documento',
    0,
    0,
    'Auditable',
    0,
    'Nota',
    NULL
WHERE NOT EXISTS (
    SELECT 1 FROM `configpayment` cp WHERE cp.maincompany_id = :maincompany_id
);

-- Cotizador
INSERT INTO `configquote` (
    maincompany_id,
    aux,
    labelaux,
    labeltax,
    requiredaux
)
SELECT
    :maincompany_id,
    0,
    NULL,
    'Impuestos',
    0
WHERE NOT EXISTS (
    SELECT 1 FROM `configquote` cq WHERE cq.maincompany_id = :maincompany_id
);

-- Facturación (tipo de cambio por defecto; moneda opcional)
INSERT INTO configbill (
    maincompany_id,
    showaprox,
    hascurrency,
    labelcurrency,
    tcurrency,
    currency_id,
    requiredcurrency
)
SELECT
    :maincompany_id,
    0,
    0,
    'Exchange rate',
    '1.000000',
    NULL,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM configbill cb WHERE cb.maincompany_id = :maincompany_id
);

-- Casilleros (configuración UI casillero / pobox)
INSERT INTO `configpobox` (
    maincompany_id,
    imagen,
    labelimagen,
    docid,
    showreempack,
    agency,
    labelagency,
    editnumber,
    custype,
    auxdate,
    username,
    checkemail,
    selfalert,
    typeselfalert,
    daysselfalert,
    phone,
    uniquedocid,
    uniquephone,
    address,
    defaultaddress,
    zip,
    defaultcity_id,
    mobile,
    requireddocid
)
SELECT
    :maincompany_id,
    0,
    'Imagen',
    0,
    0,
    0,
    'Agencia',
    0,
    0,
    0,
    0,
    0,
    0,
    'Ninguna',
    0,
    1,
    0,
    0,
    1,
    NULL,
    1,
    NULL,
    1,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM `configpobox` cpb WHERE cpb.maincompany_id = :maincompany_id
);

-- Clientes
INSERT INTO configcustomer (
    maincompany_id,
    docid,
    labeldocid,
    grouptariff,
    valdocid,
    aux,
    labelaux,
    custype,
    labelcustype,
    requiredcustype,
    listcustype,
    auxdate,
    agentautomatic,
    labelauxdate,
    requiredauxdate,
    requiredlocation,
    requiredmobile,
    requireddocid,
    uniqueemail,
    aux1,
    labelaux1,
    requiredaux1,
    labelaux2,
    aux2,
    requiredaux2,
    uniquedocid
)
SELECT
    :maincompany_id,
    0,
    'Documento de Identidad',
    0,
    'Ninguna',
    0,
    'Auxiliar',
    0,
    'Clase de Cliente',
    0,
    NULL,
    0,
    0,
    'Fecha de Cliente',
    0,
    0,
    0,
    0,
    0,
    0,
    'Auxiliar 1',
    0,
    'Auxiliar 1',
    0,
    0,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM configcustomer ccu WHERE ccu.maincompany_id = :maincompany_id
);

-- Tracking público / consulta (stateinfo serializado compatible con Configtracking::__construct)
INSERT INTO `configtracking` (
    maincompany_id,
    searchby,
    note,
    description,
    weight,
    value,
    document,
    searchall,
    statetable,
    basictable,
    stateinfo,
    basicinfo,
    details
)
SELECT
    :maincompany_id,
    'Tracking',
    0,
    0,
    0,
    0,
    0,
    0,
    1,
    0,
    'a:5:{s:5:\"fecha\";s:4:\"true\";s:6:\"status\";s:4:\"true\";s:5:\"lugar\";s:4:\"true\";s:10:\"comentario\";s:4:\"true\";s:6:\"unidad\";s:4:\"true\";}',
    NULL,
    1
WHERE NOT EXISTS (
    SELECT 1 FROM `configtracking` ct WHERE ct.maincompany_id = :maincompany_id
);

-- Dashboard operativo (flags de menú visibles)
INSERT INTO `configdash` (
    maincompany_id,
    packages,
    whrecs,
    guides,
    loadunits,
    consols,
    pickups,
    delivery,
    poboxes,
    bills,
    payments,
    graphs,
    quote,
    accounting,
    awb,
    packnew,
    packnewfast,
    packlist,
    packmanage,
    packnewprepack,
    packlistprepack,
    packsysreceipt,
    whrecnew,
    whreclist,
    whrecmanage,
    whrecinwr,
    whrecsearch,
    whrecsysreceipt,
    guidenew,
    guidelist,
    guidesearch,
    guideforpack,
    guidetoconsol,
    baglist,
    bagnew,
    bagreceive,
    bagtoconsol,
    loadunitnew,
    loadunitlist,
    consolnew,
    consollist,
    consolshipnew,
    consolshiplist,
    awbnew,
    hawbnew,
    awblist,
    blnew,
    bllist,
    poboxnew,
    poboxlist,
    alertlist,
    repacklist,
    pickupnew,
    pickuplist,
    listpickupnew,
    listpickuplist,
    deliverynew,
    deliverylist,
    billnew,
    billlist,
    billguide,
    billnoguide,
    billwhrec,
    billnowhrec,
    billvoid,
    billlogs,
    billmultipay,
    payverified,
    payrefunded,
    payvoid,
    paybyverify,
    accsuppliernew,
    accsupplierlist,
    accexpensepay,
    accexpensecat,
    accexpensenew,
    accexpenselist,
    accexpensevoid,
    accexpenselistpay,
    helps,
    whrecnewfast
)
SELECT
    :maincompany_id,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1,
    1, 1, 1, 1,
    1, 1,
    1, 1, 1, 1,
    1, 1, 1, 1, 1,
    1, 1, 1, 1,
    1, 1, 1, 1,
    1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1,
    1
WHERE NOT EXISTS (
    SELECT 1 FROM `configdash` cd WHERE cd.maincompany_id = :maincompany_id
);

-- Dashboard portal casilleros
INSERT INTO configdashpobox (
    maincompany_id,
    packages,
    whrecs,
    guides,
    trackpackages,
    repackrequest,
    repacktracking,
    repackpackages,
    repackwhrecs,
    bills,
    quote,
    billwhrecs,
    billguides,
    paymethods,
    paynotify,
    paycusver,
    paycusunver,
    accounts,
    pickups,
    alerts,
    showpobox,
    showdata,
    updatedata,
    htmlcode,
    htmlhead,
    html_mobile
)
SELECT
    :maincompany_id,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1,
    1, 1, 1, 1,
    1,
    1,
    1,
    1, 1, 1,
    NULL,
    NULL,
    NULL
WHERE NOT EXISTS (
    SELECT 1 FROM configdashpobox cdp WHERE cdp.maincompany_id = :maincompany_id
);

-- Pickups (primer tipo de envío global disponible)
INSERT INTO `configpickup` (
    maincompany_id,
    `time`,
    shippingtype_id,
    autobaddpickup,
    operationtype,
    processor,
    showsubtext,
    publicsearch,
    auxtext1,
    auxtext2,
    auxtext3,
    auxtext4,
    auxtext5,
    requestagency
)
SELECT
    :maincompany_id,
    1,
    (SELECT id FROM `shippingtype` st ORDER BY st.id ASC LIMIT 1),
    0,
    0,
    0,
    0,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM `configpickup` cpu WHERE cpu.maincompany_id = :maincompany_id
);

-- Lista de entrega
INSERT INTO configdeliverylist (
    maincompany_id,
    scanner,
    shipper,
    expdate,
    vehicle,
    requiredexpdate
)
SELECT
    :maincompany_id,
    0,
    0,
    0,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM configdeliverylist cdl WHERE cdl.maincompany_id = :maincompany_id
);

-- Mobile (app)
INSERT INTO configmobile (
    maincompany_id,
    logo
)
SELECT
    :maincompany_id,
    1
WHERE NOT EXISTS (
    SELECT 1 FROM configmobile cm WHERE cm.maincompany_id = :maincompany_id
);
