-- Migration 009: Create header_mapping_templates table
-- Requirements: 5.3, 16.4
-- Design: Data Models -> 9. HEADER MAPPING TEMPLATES
--
-- Menyimpan satu template mapping header per (user, marketplace).
-- `mapping` JSON berformat: { "internal_field": "source_column_name", ... }
-- Konstrain UNIQUE (user_id, marketplace_source) menjamin idempotensi
-- saat user re-save template untuk marketplace yang sama.

CREATE TABLE IF NOT EXISTS header_mapping_templates (
    id                  BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    user_id             BIGINT UNSIGNED NOT NULL,
    marketplace_source  ENUM('SHOPEE','TOKOPEDIA','TIKTOK_SHOP') NOT NULL,
    mapping             JSON         NOT NULL,
    created_at          DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at          DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    UNIQUE KEY uk_hmt_user_marketplace (user_id, marketplace_source),
    CONSTRAINT fk_hmt_user FOREIGN KEY (user_id)
        REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
