-- Table for Suppliers/Contractors
CREATE TABLE IF NOT EXISTS suppliers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    type ENUM('supplier', 'contractor') DEFAULT 'supplier',
    contact_info TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX (tenant_id)
);

-- Table for Expense Types (Company editable list)
CREATE TABLE IF NOT EXISTS expense_types (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX (tenant_id)
);

-- Table for Expenses
CREATE TABLE IF NOT EXISTS expenses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    project_id INT NOT NULL,
    supplier_id INT NOT NULL,
    expense_type_id INT NOT NULL,
    amount DECIMAL(15, 2) NOT NULL,
    allocation_percent DECIMAL(5, 2) DEFAULT 100.00,
    entry_date DATE NOT NULL,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    FOREIGN KEY (supplier_id) REFERENCES suppliers(id),
    FOREIGN KEY (expense_type_id) REFERENCES expense_types(id),
    INDEX (tenant_id)
);

-- Insert some default data for the demo tenant (tenant_id = 1)
INSERT INTO suppliers (tenant_id, name, type, contact_info) VALUES 
(1, 'Cloud Services Inc.', 'supplier', 'billing@cloudservices.com'),
(1, 'John Doe Consulting', 'contractor', 'john@doe.com');

INSERT INTO expense_types (tenant_id, name) VALUES 
(1, 'Materials'),
(1, 'Subcontractors'),
(1, 'Software Licenses'),
(1, 'Overhead');
