-- Multi-Outlet Implementation

-- 1. Create outlet_stock table
CREATE TABLE IF NOT EXISTS outlet_stock (
    id INT AUTO_INCREMENT PRIMARY KEY,
    outlet_id INT NOT NULL,
    item_id INT NOT NULL,
    quantity DECIMAL(15, 2) DEFAULT 0.00,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_item_outlet (outlet_id, item_id),
    CONSTRAINT fk_outlet_stock_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE CASCADE,
    CONSTRAINT fk_outlet_stock_item FOREIGN KEY (item_id) REFERENCES stock_items(id) ON DELETE CASCADE
);

-- 2. Migrate existing stock to Default Outlet (ID 1)
-- We assume Outlet 1 exists (created in previous migration)
INSERT INTO outlet_stock (outlet_id, item_id, quantity)
SELECT 1, id, stock_quantity FROM stock_items
ON DUPLICATE KEY UPDATE quantity = stock_items.stock_quantity;

-- 3. Add outlet_id to tables
-- Invoices
ALTER TABLE invoices ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE invoices ADD CONSTRAINT fk_invoices_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE invoices SET outlet_id = 1 WHERE outlet_id IS NULL;

-- POS Transactions
ALTER TABLE pos_transactions ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE pos_transactions ADD CONSTRAINT fk_pos_trans_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE pos_transactions SET outlet_id = 1 WHERE outlet_id IS NULL;

-- POS Held Carts
ALTER TABLE pos_held_carts ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE pos_held_carts ADD CONSTRAINT fk_pos_carts_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE pos_held_carts SET outlet_id = 1 WHERE outlet_id IS NULL;

-- Quotations
ALTER TABLE quotations ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE quotations ADD CONSTRAINT fk_quotations_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE quotations SET outlet_id = 1 WHERE outlet_id IS NULL;

-- LPOs
ALTER TABLE lpos ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
-- Note: lpos might already have outlet_id from a previous failed attempt or partial migration, checking IF NOT EXISTS is good.
-- We need to check if the foreign key exists before adding it to avoid errors, or just try adding it. 
-- For simplicity in this environment, we'll try to add it. If it fails, it might be due to duplicate name.
-- Let's use a safe procedure for FKs if possible, or just standard ALTER.
-- safe bet:
ALTER TABLE lpos ADD CONSTRAINT fk_lpos_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE lpos SET outlet_id = 1 WHERE outlet_id IS NULL;

-- Sales Returns
ALTER TABLE sales_returns ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE sales_returns ADD CONSTRAINT fk_sales_returns_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE sales_returns SET outlet_id = 1 WHERE outlet_id IS NULL;

-- Purchase Returns
ALTER TABLE purchase_returns ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE purchase_returns ADD CONSTRAINT fk_purchase_returns_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE purchase_returns SET outlet_id = 1 WHERE outlet_id IS NULL;

-- Expenses
ALTER TABLE expenses ADD COLUMN IF NOT EXISTS outlet_id INT DEFAULT NULL;
ALTER TABLE expenses ADD CONSTRAINT fk_expenses_outlet FOREIGN KEY (outlet_id) REFERENCES outlets(id) ON DELETE SET NULL;
UPDATE expenses SET outlet_id = 1 WHERE outlet_id IS NULL;
