-- Add batch and expiry to LPO items for receiving stock
ALTER TABLE pharmacy_lpo_items ADD COLUMN IF NOT EXISTS batch_number VARCHAR(50) NULL;
ALTER TABLE pharmacy_lpo_items ADD COLUMN IF NOT EXISTS expiry_date DATE NULL;

-- Create Purchase Returns table
CREATE TABLE IF NOT EXISTS pharmacy_purchase_returns (
    id INT AUTO_INCREMENT PRIMARY KEY,
    supplier_id INT NOT NULL,
    return_date DATE NOT NULL,
    total_amount DECIMAL(10, 2) DEFAULT 0.00,
    reason TEXT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (supplier_id) REFERENCES suppliers(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Create Purchase Return Items table
CREATE TABLE IF NOT EXISTS pharmacy_purchase_return_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    return_id INT NOT NULL,
    drug_id INT NOT NULL,
    batch_id INT NULL, -- Which batch we are returning from
    quantity INT NOT NULL,
    unit_price DECIMAL(10, 2) NOT NULL, -- Refund price
    total_price DECIMAL(10, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (return_id) REFERENCES pharmacy_purchase_returns(id) ON DELETE CASCADE,
    FOREIGN KEY (drug_id) REFERENCES drugs(id),
    FOREIGN KEY (batch_id) REFERENCES pharmacy_batches(id) ON DELETE SET NULL
);
