BEGIN;

INSERT INTO purchases.incoterms (
    tenant_id,
    code,
    name,
    buyer_responsibility,
    seller_responsibility,
    requires_freight,
    requires_insurance,
    requires_customs,
    status,
    is_active,
    is_deleted,
    metadata
)
SELECT
    t.id,
    v.code,
    v.name,
    v.buyer_responsibility,
    v.seller_responsibility,
    v.requires_freight,
    v.requires_insurance,
    v.requires_customs,
    'active',
    TRUE,
    FALSE,
    '{"source":"global_incoterms_2020_seed"}'::jsonb
FROM platform.tenants t
CROSS JOIN (VALUES
    ('N/A', 'Not Applicable / Local Purchase', 'Local purchase; buyer and seller responsibilities follow the agreed local delivery terms.', 'Local purchase; seller responsibilities follow the agreed local delivery terms.', false, false, false),
    ('EXW', 'Ex Works', 'Buyer arranges pickup, export, freight, insurance, customs, and delivery.', 'Seller makes goods available at named premises.', true, true, true),
    ('FCA', 'Free Carrier', 'Buyer arranges main carriage, insurance, import customs, and final delivery after handover.', 'Seller clears export and delivers goods to the named carrier/place.', true, true, true),
    ('FAS', 'Free Alongside Ship', 'Buyer loads goods, arranges main freight, insurance, import customs, and delivery.', 'Seller clears export and places goods alongside the vessel at the named port.', true, true, true),
    ('FOB', 'Free On Board', 'Buyer pays main freight, insurance, import duties, clearing, and inland delivery.', 'Seller clears export and loads goods on board.', true, true, true),
    ('CFR', 'Cost and Freight', 'Buyer pays insurance, import duties, clearing, and inland delivery.', 'Seller pays freight to destination port.', false, true, true),
    ('CIF', 'Cost, Insurance and Freight', 'Buyer pays import duties, clearing, and inland delivery.', 'Seller pays cost, insurance, and freight to destination port.', false, false, true),
    ('CPT', 'Carriage Paid To', 'Buyer handles insurance, import customs, and risk after goods are handed to carrier.', 'Seller pays carriage to the named destination.', false, true, true),
    ('CIP', 'Carriage and Insurance Paid To', 'Buyer handles import customs and delivery after the agreed destination point.', 'Seller pays carriage and insurance to the named destination.', false, false, true),
    ('DAP', 'Delivered at Place', 'Buyer pays import duties and taxes unless agreed otherwise.', 'Seller delivers goods to named place before import clearance.', false, false, true),
    ('DPU', 'Delivered at Place Unloaded', 'Buyer handles import clearance, duties, taxes, and onward movement after unloading.', 'Seller delivers and unloads goods at the named destination.', false, false, true),
    ('DDP', 'Delivered Duty Paid', 'Buyer receives goods with most import costs already borne by seller.', 'Seller bears freight, insurance, import duties, and delivery to named place.', false, false, false)
) AS v(code, name, buyer_responsibility, seller_responsibility, requires_freight, requires_insurance, requires_customs)
WHERE COALESCE(t.deleted_at, NULL) IS NULL
  AND NOT EXISTS (
      SELECT 1
      FROM purchases.incoterms existing
      WHERE existing.tenant_id = t.id
        AND existing.code = v.code
        AND COALESCE(existing.is_deleted, FALSE) = FALSE
  );

COMMIT;
