ALTER TABLE products
    ADD COLUMN IF NOT EXISTS hero_eyebrow VARCHAR(255) NULL AFTER slug,
    ADD COLUMN IF NOT EXISTS hero_media_id INT UNSIGNED NULL AFTER short_description,
    ADD COLUMN IF NOT EXISTS hero_cta_label VARCHAR(255) NULL AFTER hero_media_id,
    ADD COLUMN IF NOT EXISTS hero_cta_url VARCHAR(255) NULL AFTER hero_cta_label,
    ADD COLUMN IF NOT EXISTS hero_secondary_cta_label VARCHAR(255) NULL AFTER hero_cta_url,
    ADD COLUMN IF NOT EXISTS hero_secondary_cta_url VARCHAR(255) NULL AFTER hero_secondary_cta_label,
    ADD COLUMN IF NOT EXISTS hero_anchor VARCHAR(120) NULL AFTER hero_secondary_cta_url;

CREATE TABLE IF NOT EXISTS entity_sections (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    owner_type VARCHAR(32) NOT NULL,
    owner_id INT UNSIGNED NOT NULL,
    section_type VARCHAR(64) NOT NULL,
    title VARCHAR(255) NULL,
    eyebrow VARCHAR(255) NULL,
    body LONGTEXT NULL,
    settings_json LONGTEXT NULL,
    sort_order INT NOT NULL DEFAULT 0,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_entity_sections_owner (owner_type, owner_id, sort_order, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS entity_relations (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    source_type VARCHAR(32) NOT NULL,
    source_id INT UNSIGNED NOT NULL,
    target_type VARCHAR(32) NOT NULL,
    target_id INT UNSIGNED NOT NULL,
    sort_order INT NOT NULL DEFAULT 0,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_entity_relations_pair (source_type, source_id, target_type, target_id),
    KEY idx_entity_relations_source (source_type, source_id, sort_order, id),
    KEY idx_entity_relations_target (target_type, target_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
