CREATE TABLE transaction_draft (
    id INTEGER PRIMARY KEY,
    added_at TEXT NOT NULL,
    template_name TEXT,
    timestamp TEXT,
    amount INTEGER,
    currency TEXT,
    description TEXT,
    internal_transfer BOOLEAN DEFAULT FALSE,
    vendor_id INTEGER,
    category_id INTEGER,
    credited_account_id INTEGER,
    debited_account_id INTEGER,
    CONSTRAINT fk_transaction_draft_vendor
        FOREIGN KEY (vendor_id) REFERENCES transaction_vendor(id)
            ON UPDATE CASCADE ON DELETE SET NULL,
    CONSTRAINT fk_transaction_draft_category
        FOREIGN KEY (category_id) REFERENCES transaction_category(id)
            ON UPDATE CASCADE ON DELETE SET NULL,
    CONSTRAINT fk_transaction_draft_credited_account
        FOREIGN KEY (credited_account_id) REFERENCES account(id)
            ON UPDATE CASCADE ON DELETE SET NULL,
    CONSTRAINT fk_transaction_draft_debited_account
        FOREIGN KEY (debited_account_id) REFERENCES account(id)
            ON UPDATE CASCADE ON DELETE SET NULL,
    CONSTRAINT ck_transaction_amount_positive
        CHECK (amount IS NULL OR amount > 0)
);

CREATE TABLE transaction_draft_tag (
    draft_id INTEGER NOT NULL,
    tag TEXT NOT NULL,
    CONSTRAINT pk_transaction_draft_tag PRIMARY KEY (draft_id, tag),
    CONSTRAINT fk_transaction_draft_tag_draft
        FOREIGN KEY (draft_id) REFERENCES transaction_draft(id)
            ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE transaction_draft_attachment (
    draft_id INTEGER NOT NULL,
    attachment_id INTEGER NOT NULL,
    PRIMARY KEY (draft_id, attachment_id),
    CONSTRAINT fk_transaction_draft_attachment_transaction
        FOREIGN KEY (draft_id) REFERENCES transaction_draft(id)
            ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT fk_transaction_draft_attachment_attachment
        FOREIGN KEY (attachment_id) REFERENCES attachment(id)
            ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE transaction_draft_line_item (
    draft_id INTEGER NOT NULL,
    idx INTEGER NOT NULL DEFAULT 0,
    value_per_item INTEGER NOT NULL,
    quantity INTEGER NOT NULL DEFAULT 1,
    description TEXT NOT NULL,
    category_id INTEGER,
    CONSTRAINT pk_transaction_draft_line_item PRIMARY KEY (draft_id, idx),
    CONSTRAINT fk_transaction_draft_line_item_transaction
        FOREIGN KEY (draft_id) REFERENCES transaction_draft(id)
            ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT fk_transaction_draft_line_item_category
        FOREIGN KEY (category_id) REFERENCES transaction_category(id)
            ON UPDATE CASCADE ON DELETE SET NULL
);

CREATE TABLE recurring_transaction (
    id INTEGER PRIMARY KEY,
    draft_id INTEGER NOT NULL,
    schedule_expr TEXT NOT NULL,
    CONSTRAINT fk_recurring_transaction_draft
        FOREIGN KEY (draft_id) REFERENCES transaction_draft(id)
            ON UPDATE CASCADE ON DELETE CASCADE
);

-- A "queue" table where upcoming recurring transactions are posted until it's
-- time to add them for real. This ensures proper handling of recurring transactions.
CREATE TABLE recurring_transaction_queue (
    recurring_transaction_id INTEGER PRIMARY KEY,
    scheduled_timestamp TEXT NOT NULL,
    CONSTRAINT fk_recurring_transaction_queue_id
        FOREIGN KEY (recurring_transaction_id) REFERENCES recurring_transaction(id)
            ON UPDATE CASCADE ON DELETE CASCADE
);
