CREATE TABLE IF NOT EXISTS product_meta (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    product_id BIGINT UNSIGNED NOT NULL,
    meta_key VARCHAR(120) NOT NULL,
    meta_value MEDIUMTEXT NULL,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY product_meta_product_key_unique (product_id, meta_key),
    CONSTRAINT product_meta_product_fk FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS product_variants (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    product_id BIGINT UNSIGNED NOT NULL,
    color_name VARCHAR(120) NULL,
    color_hex VARCHAR(20) NULL,
    memory_label VARCHAR(120) NULL,
    sku VARCHAR(140) NULL,
    price DECIMAL(12,2) NOT NULL DEFAULT 0,
    sale_price DECIMAL(12,2) NULL,
    stock_quantity INT NOT NULL DEFAULT 0,
    image_id BIGINT UNSIGNED NULL,
    sort_order INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX product_variants_product_idx (product_id),
    CONSTRAINT product_variants_product_fk FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
    CONSTRAINT product_variants_image_fk FOREIGN KEY (image_id) REFERENCES product_images(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Mobilni telefoni', 'mobilni-telefoni', NULL, 10, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'mobilni-telefoni');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Laptopi i tableti', 'laptopi-i-tableti', NULL, 20, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'laptopi-i-tableti');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Satovi', 'satovi', NULL, 30, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'satovi');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Dodatna oprema', 'dodatna-oprema', NULL, 40, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'dodatna-oprema');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Maske', 'maske', (SELECT id FROM categories WHERE slug = 'dodatna-oprema' LIMIT 1), 41, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'maske');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Punjači', 'punjaci', (SELECT id FROM categories WHERE slug = 'dodatna-oprema' LIMIT 1), 42, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'punjaci');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Slušalice', 'slusalice', (SELECT id FROM categories WHERE slug = 'dodatna-oprema' LIMIT 1), 43, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'slusalice');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Zaštitna stakla', 'zastitna-stakla', (SELECT id FROM categories WHERE slug = 'dodatna-oprema' LIMIT 1), 44, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'zastitna-stakla');

INSERT INTO categories (name, slug, parent_id, sort_order, is_active)
SELECT 'Ostalo', 'ostalo', (SELECT id FROM categories WHERE slug = 'dodatna-oprema' LIMIT 1), 45, 1
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'ostalo');