CREATE TABLE IF NOT EXISTS carrier_delivery_method_mappings (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    source_system VARCHAR(32) NOT NULL,
    source_integration_id INT UNSIGNED NOT NULL DEFAULT 0,
    order_delivery_method VARCHAR(200) NOT NULL,
    provider VARCHAR(50) NOT NULL,
    provider_service_id VARCHAR(128) NOT NULL,
    provider_account_id VARCHAR(128) NULL,
    provider_carrier_id VARCHAR(128) NULL,
    provider_service_name VARCHAR(255) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY carrier_dm_mapping_unique (source_system, source_integration_id, order_delivery_method),
    KEY carrier_dm_mapping_provider_idx (provider),
    KEY carrier_dm_mapping_source_idx (source_system, source_integration_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO carrier_delivery_method_mappings (
    source_system,
    source_integration_id,
    order_delivery_method,
    provider,
    provider_service_id,
    provider_account_id,
    provider_carrier_id,
    provider_service_name,
    created_at,
    updated_at
)
SELECT
    'allegro' AS source_system,
    0 AS source_integration_id,
    m.order_delivery_method,
    'allegro_wza' AS provider,
    m.allegro_delivery_method_id AS provider_service_id,
    NULLIF(m.allegro_credentials_id, '') AS provider_account_id,
    NULLIF(m.allegro_carrier_id, '') AS provider_carrier_id,
    NULLIF(m.allegro_service_name, '') AS provider_service_name,
    NOW(),
    NOW()
FROM allegro_delivery_method_mappings m
LEFT JOIN carrier_delivery_method_mappings n
    ON n.source_system = 'allegro'
    AND n.source_integration_id = 0
    AND n.order_delivery_method = m.order_delivery_method
WHERE n.id IS NULL
  AND m.order_delivery_method IS NOT NULL
  AND m.order_delivery_method <> ''
  AND m.allegro_delivery_method_id IS NOT NULL
  AND m.allegro_delivery_method_id <> '';

INSERT INTO carrier_delivery_method_mappings (
    source_system,
    source_integration_id,
    order_delivery_method,
    provider,
    provider_service_id,
    provider_account_id,
    provider_carrier_id,
    provider_service_name,
    created_at,
    updated_at
)
SELECT
    'shoppro' AS source_system,
    m.integration_id AS source_integration_id,
    m.order_delivery_method,
    'allegro_wza' AS provider,
    m.allegro_delivery_method_id AS provider_service_id,
    NULLIF(m.allegro_credentials_id, '') AS provider_account_id,
    NULLIF(m.allegro_carrier_id, '') AS provider_carrier_id,
    NULLIF(m.allegro_service_name, '') AS provider_service_name,
    NOW(),
    NOW()
FROM shoppro_delivery_method_mappings m
LEFT JOIN carrier_delivery_method_mappings n
    ON n.source_system = 'shoppro'
    AND n.source_integration_id = m.integration_id
    AND n.order_delivery_method = m.order_delivery_method
WHERE n.id IS NULL
  AND m.order_delivery_method IS NOT NULL
  AND m.order_delivery_method <> ''
  AND m.allegro_delivery_method_id IS NOT NULL
  AND m.allegro_delivery_method_id <> '';
