-- Vendor is a subledger, just like a student
CREATE TABLE IF NOT EXISTS vendors (
    vendor_id VARCHAR(36) PRIMARY KEY,
    subledger_id VARCHAR(36) NOT NULL,
    vendor_name VARCHAR(255) NOT NULL,
    contact_person VARCHAR(255),
    email VARCHAR(255),
    phone VARCHAR(50),
    address TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (subledger_id) REFERENCES subledgers(subledger_id)
);

-- A bill is an operational document from a vendor
CREATE TABLE IF NOT EXISTS bills (
    bill_id VARCHAR(36) PRIMARY KEY,
    vendor_id VARCHAR(36) NOT NULL,
    journal_entry_id VARCHAR(36),
    bill_date DATE NOT NULL,
    due_date DATE NOT NULL,
    total_amount DECIMAL(18, 2) NOT NULL,
    status ENUM('DRAFT', 'SUBMITTED', 'PAID', 'VOID') NOT NULL DEFAULT 'DRAFT',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (vendor_id) REFERENCES vendors(vendor_id),
    FOREIGN KEY (journal_entry_id) REFERENCES journal_entries(journal_entry_id)
);

-- Lines of a bill, detailing the expenses
CREATE TABLE IF NOT EXISTS bill_lines (
    bill_line_id VARCHAR(36) PRIMARY KEY,
    bill_id VARCHAR(36) NOT NULL,
    expense_account_code VARCHAR(20) NOT NULL,
    description TEXT,
    amount DECIMAL(18, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (bill_id) REFERENCES bills(bill_id),
    FOREIGN KEY (expense_account_code) REFERENCES accounts(account_code)
);
