-- Create tables if they don't exist
CREATE TABLE IF NOT EXISTS products (
    product_id BIGINT NOT NULL,
    title VARCHAR(255) NOT NULL,
    SKU VARCHAR(50) NOT NULL,
    created_at TIMESTAMP NULL,
    stock_quantity INT DEFAULT 0,
    price DECIMAL(10, 3) NOT NULL,
    regular_price DECIMAL(10, 3) NOT NULL,
    cost_price DECIMAL(10, 3),
    landing_cost_price DECIMAL(10, 3),
    barcode VARCHAR(50),
    updated_at TIMESTAMP,
    visible BOOLEAN DEFAULT true,
    managing_stock BOOLEAN DEFAULT true,
    replenishable BOOLEAN DEFAULT true,
    vendor VARCHAR(100),
    vendor_reference VARCHAR(100),
    permalink VARCHAR(255),
    categories TEXT,
    image VARCHAR(255),
    brand VARCHAR(100),
    options TEXT,
    tags TEXT,
    moq INT DEFAULT 1,
    uom INT DEFAULT 1,
    PRIMARY KEY (product_id),
    UNIQUE KEY unique_sku (SKU),
    INDEX idx_vendor (vendor),
    INDEX idx_brand (brand)
);

CREATE TABLE IF NOT EXISTS orders (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    order_number VARCHAR(50) NOT NULL,
    product_id BIGINT NOT NULL,
    SKU VARCHAR(50) NOT NULL,
    date DATE NOT NULL,
    price DECIMAL(10, 3) NOT NULL,
    quantity INT NOT NULL,
    discount DECIMAL(10, 3) DEFAULT 0,
    tax DECIMAL(10, 3) DEFAULT 0,
    tax_included BOOLEAN DEFAULT false,
    shipping DECIMAL(10, 3) DEFAULT 0,
    customer VARCHAR(50) NOT NULL,
    status VARCHAR(20) DEFAULT 'pending',
    payment_method VARCHAR(50),
    shipping_method VARCHAR(50),
    shipping_address TEXT,
    billing_address TEXT,
    canceled BOOLEAN DEFAULT false,
    FOREIGN KEY (product_id) REFERENCES products(product_id),
    FOREIGN KEY (SKU) REFERENCES products(SKU),
    INDEX idx_order_number (order_number),
    INDEX idx_customer (customer),
    INDEX idx_date (date),
    INDEX idx_status (status),
    UNIQUE KEY unique_order_product (order_number, product_id)
);

CREATE TABLE IF NOT EXISTS purchase_orders (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    po_id VARCHAR(50) NOT NULL,
    vendor VARCHAR(100) NOT NULL,
    date DATE NOT NULL,
    expected_date DATE,
    product_id BIGINT NOT NULL,
    sku VARCHAR(50) NOT NULL,
    cost_price DECIMAL(10, 3) NOT NULL,
    status VARCHAR(20) DEFAULT 'pending',
    notes TEXT,
    ordered INT NOT NULL,
    received INT DEFAULT 0,
    received_date DATE,
    FOREIGN KEY (product_id) REFERENCES products(product_id),
    FOREIGN KEY (sku) REFERENCES products(SKU),
    INDEX idx_po_id (po_id),
    INDEX idx_vendor (vendor),
    INDEX idx_status (status),
    UNIQUE KEY unique_po_product (po_id, product_id)
);

-- Create categories table
CREATE TABLE IF NOT EXISTS categories (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_name (name)
);

-- Create product_categories junction table
CREATE TABLE IF NOT EXISTS product_categories (
    product_id BIGINT NOT NULL,
    category_id BIGINT NOT NULL,
    PRIMARY KEY (product_id, category_id),
    FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE,
    FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
    INDEX idx_category (category_id),
    INDEX idx_product (product_id)
);

-- Create views for common calculations
CREATE OR REPLACE VIEW product_sales_trends AS
SELECT 
    p.product_id,
    p.SKU,
    p.title,
    COALESCE(SUM(o.quantity), 0) as total_sold,
    COALESCE(AVG(o.quantity), 0) as avg_quantity_per_order,
    COALESCE(COUNT(DISTINCT o.order_number), 0) as number_of_orders,
    MIN(o.date) as first_sale_date,
    MAX(o.date) as last_sale_date
FROM 
    products p
LEFT JOIN 
    orders o ON p.product_id = o.product_id
WHERE 
    o.canceled = false
GROUP BY 
    p.product_id, p.SKU, p.title;

-- Create view for inventory health
CREATE OR REPLACE VIEW inventory_health AS
SELECT 
    p.product_id,
    p.SKU,
    p.title,
    p.stock_quantity,
    pm.daily_sales_avg,
    pm.days_of_inventory,
    pm.reorder_point,
    pm.safety_stock,
    CASE 
        WHEN p.stock_quantity <= pm.safety_stock THEN 'Critical'
        WHEN p.stock_quantity <= pm.reorder_point THEN 'Reorder'
        WHEN p.stock_quantity > (pm.daily_sales_avg * 90) THEN 'Overstocked'
        ELSE 'Healthy'
    END as stock_status
FROM 
    products p
LEFT JOIN 
    product_metrics pm ON p.product_id = pm.product_id
WHERE 
    p.managing_stock = true; 