-- +goose Up
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE budgets (
    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
    name text NOT NULL,
    last_modification timestamp with time zone
);

CREATE TABLE users (
    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
    email text NOT NULL,
    name text NOT NULL,
    password text NOT NULL
);

CREATE TABLE user_budgets (
    user_id uuid NOT NULL,
    budget_id uuid NOT NULL
);
ALTER TABLE "user_budgets" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "user_budgets" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");

CREATE TABLE accounts (
        id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
        budget_id uuid NOT NULL,
        name varchar(50) NOT NULL
);
ALTER TABLE "accounts" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");

CREATE TABLE payees (
        id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
        budget_id uuid NOT NULL,
        name varchar(50) NOT NULL
);

CREATE TABLE transactions (
    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
    date date NOT NULL,
    memo text NOT NULL,
    amount decimal(12,2) NOT NULL,
    account_id uuid NOT NULL,
    payee_id uuid
);
ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id");
ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id");

-- +goose Down
DROP EXTENSION "uuid-ossp";
DROP TABLE budgets;
DROP TABLE users;
DROP TABLE user_budgets;
DROP TABLE transactions;
DROP TABLE accounts;
DROP TABLE payees;