CREATE TABLE IF NOT EXISTS attributes (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    type TINYINT UNSIGNED NOT NULL DEFAULT 1,
    status TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY attributes_status_idx (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS attribute_translations (
    attribute_id INT UNSIGNED NOT NULL,
    lang VARCHAR(8) NOT NULL,
    name VARCHAR(255) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (attribute_id, lang),
    KEY attribute_translations_lang_name_idx (lang, name),
    CONSTRAINT attribute_translations_attribute_fk
        FOREIGN KEY (attribute_id) REFERENCES attributes(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS attribute_values (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    attribute_id INT UNSIGNED NOT NULL,
    status TINYINT(1) NOT NULL DEFAULT 1,
    is_default TINYINT(1) NOT NULL DEFAULT 0,
    impact_on_price DECIMAL(12,2) NULL,
    sort_order INT NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY attribute_values_attribute_idx (attribute_id),
    KEY attribute_values_default_idx (attribute_id, is_default),
    CONSTRAINT attribute_values_attribute_fk
        FOREIGN KEY (attribute_id) REFERENCES attributes(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS attribute_value_translations (
    value_id INT UNSIGNED NOT NULL,
    lang VARCHAR(8) NOT NULL,
    name VARCHAR(255) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (value_id, lang),
    KEY attribute_value_translations_lang_name_idx (lang, name),
    CONSTRAINT attribute_value_translations_value_fk
        FOREIGN KEY (value_id) REFERENCES attribute_values(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
