CREATE DATABASE IF NOT EXISTS argent_shard_01
CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
USE argent_shard_01;

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

CREATE TABLE ledger (
    entry_id BINARY(16) NOT NULL,
    batch_id BINARY(16) NOT NULL,
    user_id BINARY(16) NOT NULL,
    group_id BINARY(16) 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 BINARY(16) 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');

DELIMITER //
CREATE TRIGGER after_ledger_insert
AFTER INSERT ON ledger
FOR EACH ROW
BEGIN
    IF NEW.status_id = 1 THEN
        UPDATE user_balances
        SET total_pending = total_pending + NEW.amount
        WHERE user_id = NEW.user_id AND currency_code = NEW.currency_code;
    ELSEIF NEW.status_id IN (2, 4) THEN
        UPDATE user_balances
        SET total_confirmed = total_confirmed + NEW.amount
        WHERE user_id = NEW.user_id AND currency_code = NEW.currency_code;
    END IF;
END //
CREATE TRIGGER after_ledger_update
AFTER UPDATE ON ledger
FOR EACH ROW
BEGIN
    IF OLD.status_id = 1 THEN
        UPDATE user_balances SET total_pending = total_pending - OLD.amount
        WHERE user_id = OLD.user_id AND currency_code = OLD.currency_code;
    ELSEIF OLD.status_id IN (2, 4) THEN
        UPDATE user_balances SET total_confirmed = total_confirmed - OLD.amount
        WHERE user_id = OLD.user_id AND currency_code = OLD.currency_code;
    END IF;
    IF NEW.status_id = 1 THEN
        UPDATE user_balances SET total_pending = total_pending + NEW.amount
        WHERE user_id = NEW.user_id AND currency_code = NEW.currency_code;
    ELSEIF NEW.status_id IN (2, 4) THEN
        UPDATE user_balances SET total_confirmed = total_confirmed + NEW.amount
        WHERE user_id = NEW.user_id AND currency_code = NEW.currency_code;
    END IF;
END //
CREATE TRIGGER after_ledger_delete
AFTER DELETE ON ledger
FOR EACH ROW
BEGIN
    IF OLD.status_id = 1 THEN
        UPDATE user_balances SET total_pending = total_pending - OLD.amount
        WHERE user_id = OLD.user_id AND currency_code = OLD.currency_code;
    ELSEIF OLD.status_id IN (2, 4) THEN
        UPDATE user_balances SET total_confirmed = total_confirmed - OLD.amount
        WHERE user_id = OLD.user_id AND currency_code = OLD.currency_code;
    END IF;
END //
DELIMITER ;


