-- Migration: Add model table and update product table
-- Create model table
CREATE TABLE IF NOT EXISTS model (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    category_id INTEGER NOT NULL,
    brand VARCHAR(100) NOT NULL,
    base_price DECIMAL(10, 2),
    sizes JSONB,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (category_id) REFERENCES category(id) ON DELETE CASCADE,
    UNIQUE (name, category_id, brand)
);

CREATE INDEX IF NOT EXISTS idx_model_category ON model(category_id);
CREATE INDEX IF NOT EXISTS idx_model_brand ON model(brand);

-- Add new columns to product table
ALTER TABLE product ADD COLUMN IF NOT EXISTS model_id INTEGER REFERENCES model(id) ON DELETE SET NULL;
ALTER TABLE product ADD COLUMN IF NOT EXISTS override_price DECIMAL(10, 2);
ALTER TABLE product ADD COLUMN IF NOT EXISTS override_sizes JSONB;

CREATE INDEX IF NOT EXISTS idx_product_model ON product(model_id);
