-- Migration 110: Product suppliers
-- Track multiple suppliers per product

CREATE TABLE IF NOT EXISTS product_suppliers (
    id SERIAL PRIMARY KEY,
    product_id INTEGER NOT NULL REFERENCES products(id) ON DELETE CASCADE,
    supplier_name VARCHAR(255),
    supplier_code VARCHAR(100),
    supplier_sku VARCHAR(100),
    supplier_price DECIMAL(10, 2),
    supplier_currency VARCHAR(10) DEFAULT 'DKK',
    supplier_stock INTEGER,
    supplier_url TEXT,
    supplier_product_url TEXT,
    source VARCHAR(50) DEFAULT 'manual',
    last_updated_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_product_suppliers_product
    ON product_suppliers(product_id);

CREATE INDEX IF NOT EXISTS idx_product_suppliers_supplier
    ON product_suppliers(supplier_code, supplier_sku);

CREATE TRIGGER trigger_product_suppliers_updated_at
BEFORE UPDATE ON product_suppliers
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
