CREATE TABLE IF NOT EXISTS products (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    uuid CHAR(36) NOT NULL,
    type ENUM('simple','variant_parent') NOT NULL DEFAULT 'simple',
    sku VARCHAR(128) NULL,
    ean VARCHAR(32) NULL,
    status TINYINT(1) NOT NULL DEFAULT 1,
    promoted TINYINT(1) NOT NULL DEFAULT 0,
    vat DECIMAL(5,2) NULL,
    weight DECIMAL(10,3) NULL,
    price_brutto DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    price_brutto_promo DECIMAL(12,2) NULL,
    price_netto DECIMAL(12,2) NULL,
    price_netto_promo DECIMAL(12,2) NULL,
    quantity DECIMAL(12,3) NOT NULL DEFAULT 0.000,
    producer_id INT UNSIGNED NULL,
    product_unit_id INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at DATETIME NULL,
    UNIQUE KEY products_uuid_unique (uuid),
    UNIQUE KEY products_sku_unique (sku),
    KEY products_status_idx (status),
    KEY products_type_idx (type),
    KEY products_updated_at_idx (updated_at),
    KEY products_ean_idx (ean)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS product_translations (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    product_id INT UNSIGNED NOT NULL,
    lang VARCHAR(8) NOT NULL,
    name VARCHAR(255) NOT NULL,
    short_description TEXT NULL,
    description LONGTEXT NULL,
    meta_title VARCHAR(255) NULL,
    meta_description VARCHAR(255) NULL,
    meta_keywords VARCHAR(255) NULL,
    seo_link VARCHAR(255) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY product_translations_product_lang_unique (product_id, lang),
    KEY product_translations_lang_name_idx (lang, name),
    CONSTRAINT product_translations_product_fk
        FOREIGN KEY (product_id) REFERENCES products(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS product_images (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    product_id INT UNSIGNED NOT NULL,
    storage_path VARCHAR(255) NOT NULL,
    alt VARCHAR(255) NULL,
    sort_order INT NOT NULL DEFAULT 0,
    is_main TINYINT(1) 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 product_images_product_sort_idx (product_id, sort_order),
    KEY product_images_main_idx (product_id, is_main),
    CONSTRAINT product_images_product_fk
        FOREIGN KEY (product_id) REFERENCES products(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS product_categories (
    product_id INT UNSIGNED NOT NULL,
    category_id INT UNSIGNED NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (product_id, category_id),
    KEY product_categories_category_idx (category_id),
    CONSTRAINT product_categories_product_fk
        FOREIGN KEY (product_id) REFERENCES products(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
