ALTER TABLE product_channel_map
    ADD COLUMN integration_id INT UNSIGNED NULL AFTER channel_id,
    ADD COLUMN link_type VARCHAR(32) NOT NULL DEFAULT 'manual' AFTER sync_state,
    ADD COLUMN link_status VARCHAR(32) NOT NULL DEFAULT 'active' AFTER link_type,
    ADD COLUMN confidence TINYINT UNSIGNED NULL AFTER link_status,
    ADD COLUMN linked_at DATETIME NULL AFTER confidence,
    ADD COLUMN linked_by_user_id INT UNSIGNED NULL AFTER linked_at,
    ADD COLUMN unlinked_at DATETIME NULL AFTER linked_by_user_id,
    ADD COLUMN unlinked_by_user_id INT UNSIGNED NULL AFTER unlinked_at,
    ADD COLUMN sync_meta_json JSON NULL AFTER unlinked_by_user_id,
    ADD KEY product_channel_map_integration_external_idx (integration_id, external_product_id, external_variant_id),
    ADD KEY product_channel_map_product_link_status_idx (product_id, link_status),
    ADD KEY product_channel_map_channel_link_status_idx (channel_id, link_status),
    ADD KEY product_channel_map_linked_by_user_idx (linked_by_user_id),
    ADD KEY product_channel_map_unlinked_by_user_idx (unlinked_by_user_id),
    ADD CONSTRAINT product_channel_map_integration_fk
        FOREIGN KEY (integration_id) REFERENCES integrations(id)
        ON DELETE SET NULL ON UPDATE CASCADE,
    ADD CONSTRAINT product_channel_map_linked_by_user_fk
        FOREIGN KEY (linked_by_user_id) REFERENCES users(id)
        ON DELETE SET NULL ON UPDATE CASCADE,
    ADD CONSTRAINT product_channel_map_unlinked_by_user_fk
        FOREIGN KEY (unlinked_by_user_id) REFERENCES users(id)
        ON DELETE SET NULL ON UPDATE CASCADE;

CREATE TABLE IF NOT EXISTS channel_offers (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    integration_id INT UNSIGNED NOT NULL,
    channel_id INT UNSIGNED NOT NULL,
    external_product_id VARCHAR(128) NOT NULL,
    external_variant_id VARCHAR(128) NULL,
    external_offer_id VARCHAR(128) NULL,
    name VARCHAR(255) NOT NULL,
    sku VARCHAR(128) NULL,
    ean VARCHAR(32) NULL,
    price_brutto DECIMAL(12,2) NULL,
    quantity DECIMAL(12,3) NULL,
    currency VARCHAR(8) NULL,
    offer_status VARCHAR(32) NOT NULL DEFAULT 'active',
    source_updated_at DATETIME NULL,
    last_seen_at DATETIME NOT NULL,
    payload_json JSON NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY channel_offers_unique_external (integration_id, external_product_id, external_variant_id),
    KEY channel_offers_integration_channel_idx (integration_id, channel_id),
    KEY channel_offers_sku_idx (sku),
    KEY channel_offers_ean_idx (ean),
    KEY channel_offers_offer_status_idx (offer_status),
    KEY channel_offers_last_seen_idx (last_seen_at),
    CONSTRAINT channel_offers_integration_fk
        FOREIGN KEY (integration_id) REFERENCES integrations(id)
        ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT channel_offers_channel_fk
        FOREIGN KEY (channel_id) REFERENCES sales_channels(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS product_link_events (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    product_channel_map_id INT UNSIGNED NOT NULL,
    event_type VARCHAR(32) NOT NULL,
    before_json JSON NULL,
    after_json JSON NULL,
    created_by_user_id INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY product_link_events_map_idx (product_channel_map_id),
    KEY product_link_events_event_type_idx (event_type),
    KEY product_link_events_created_by_user_idx (created_by_user_id),
    KEY product_link_events_created_at_idx (created_at),
    CONSTRAINT product_link_events_map_fk
        FOREIGN KEY (product_channel_map_id) REFERENCES product_channel_map(id)
        ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT product_link_events_created_by_user_fk
        FOREIGN KEY (created_by_user_id) REFERENCES users(id)
        ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
