CREATE TABLE IF NOT EXISTS qt_competitors (
    id                    BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name                  VARCHAR(255) NOT NULL,
    code                  VARCHAR(100) NOT NULL UNIQUE,
    delivery_basis        VARCHAR(50)  NOT NULL DEFAULT 'DDP',
    expected_discount_pct DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    column_mapping        JSON         NULL,
    is_active             BOOLEAN      NOT NULL DEFAULT TRUE,
    created_at            TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at            TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS partnumber_log_competitors (
    id              BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    competitor_id   BIGINT UNSIGNED NOT NULL,
    lot_name        VARCHAR(255)    NOT NULL,
    raw_partnumber  VARCHAR(255)    NULL,
    price           DECIMAL(12,2)   NOT NULL,
    quote_date      DATE            NOT NULL,
    created_at      TIMESTAMP       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_plc_competitor_id (competitor_id),
    INDEX idx_plc_lot_name (lot_name),
    INDEX idx_plc_competitor_lot_date (competitor_id, lot_name, quote_date),
    CONSTRAINT fk_plc_competitor
        FOREIGN KEY (competitor_id) REFERENCES qt_competitors(id)
        ON DELETE RESTRICT ON UPDATE CASCADE
);
