-- This schema is included at compile-time into data : SqliteDataSource.

-- Basic/Utility Entities

CREATE TABLE profile_property (
    property TEXT PRIMARY KEY,
    value TEXT DEFAULT NULL
);

CREATE TABLE attachment (
    id INTEGER PRIMARY KEY,
    uploaded_at TEXT NOT NULL,
    filename TEXT NOT NULL,
    content_type TEXT NOT NULL,
    size INTEGER NOT NULL,
    content BLOB NOT NULL
);

-- Account Entities

CREATE TABLE account (
    id INTEGER PRIMARY KEY,
    created_at TEXT NOT NULL,
    archived BOOLEAN NOT NULL DEFAULT FALSE,
    type TEXT NOT NULL,
    number_suffix TEXT,
    name TEXT NOT NULL,
    currency TEXT NOT NULL,
    description TEXT
);

CREATE TABLE account_credit_card_properties (
    account_id INTEGER PRIMARY KEY,
    credit_limit TEXT,
    CONSTRAINT fk_account_credit_card_properties_account
        FOREIGN KEY (account_id) REFERENCES account(id)
            ON UPDATE CASCADE ON DELETE CASCADE
);

-- Transaction Entities

CREATE TABLE transaction_vendor (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    description TEXT
);

CREATE TABLE transaction_category (
    id INTEGER PRIMARY KEY,
    parent_id INTEGER,
    name TEXT NOT NULL UNIQUE,
    color TEXT NOT NULL DEFAULT 'FFFFFF',
    CONSTRAINT fk_transaction_category_parent
        FOREIGN KEY (parent_id) REFERENCES transaction_category(id)
            ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE transaction_tag (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE
);

CREATE TABLE "transaction" (
    id INTEGER PRIMARY KEY,
    timestamp TEXT NOT NULL,
    added_at TEXT NOT NULL,
    amount TEXT NOT NULL,
    currency TEXT NOT NULL,
    description TEXT,
    vendor_id INTEGER,
    category_id INTEGER,
    CONSTRAINT fk_transaction_vendor
        FOREIGN KEY (vendor_id) REFERENCES transaction_vendor(id)
            ON UPDATE CASCADE ON DELETE SET NULL,
    CONSTRAINT fk_transaction_category
        FOREIGN KEY (category_id) REFERENCES transaction_category(id)
            ON UPDATE CASCADE ON DELETE SET NULL
);


