CREATE TABLE IF NOT EXISTS journal_lines (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    journal_entry_id UUID NOT NULL REFERENCES journal_entries(id) ON DELETE CASCADE,
    account_id UUID NOT NULL REFERENCES accounts(id),
    description TEXT,
    debit NUMERIC(19, 4),
    credit NUMERIC(19, 4),
    CONSTRAINT check_debit_credit CHECK (
        (debit IS NOT NULL AND credit IS NULL) OR
        (debit IS NULL AND credit IS NOT NULL) OR
        (debit IS NULL AND credit IS NULL)
    )
);

CREATE INDEX idx_journal_lines_entry ON journal_lines(journal_entry_id);
CREATE INDEX idx_journal_lines_account ON journal_lines(account_id);
