CREATE DATABASE IF NOT EXISTS argent_shard_01;
USE argent_shard_01;

CREATE TABLE ledger_statuses (
    status_id TINYINT PRIMARY KEY,
    name VARCHAR(20) UNIQUE NOT NULL
);

CREATE TABLE ledger (
    entry_id CHAR(36) NOT NULL,
    batch_id CHAR(36) NOT NULL,
    user_id CHAR(36) NOT NULL,
    group_id CHAR(36) DEFAULT NULL,
    category_id INT,
    amount DECIMAL(15, 2) NOT NULL,
    currency_code CHAR(3) NOT NULL,
    description VARCHAR(255),
    status_id TINYINT DEFAULT '1',
    created_at DATETIME NOT NULL,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    PRIMARY KEY (user_id, entry_id),
    FOREIGN KEY (status_id) REFERENCES ledger_statuses(status_id),
    INDEX (batch_id),
    INDEX (group_id),
    INDEX (status_id)
);

CREATE TABLE user_balances (
    user_id CHAR(36) NOT NULL,
    currency_code CHAR(3) NOT NULL,
    total_confirmed DECIMAL(15, 2) DEFAULT 0.00,
    total_pending DECIMAL(15, 2) DEFAULT 0.00,
    PRIMARY KEY (user_id, currency_code)
);

INSERT INTO ledger_statuses (status_id, name) VALUES
(1, 'proposed'),
(2, 'ratified'),
(3, 'disputed'),
(4, 'settled');
