CREATE TABLE IF NOT EXISTS
    public.status (
        id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
        name TEXT NOT NULL,
        created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
        updated_at TIMESTAMP WITH TIME ZONE NULL
    );

CREATE UNIQUE INDEX IF NOT EXISTS status_name_uniq_idx ON public.status(name);

INSERT INTO
    public.status (
        name
    )
VALUES
    ('Active'),
    ('Unverified'),
    ('Removed'),
    ('Quarantined');

CREATE TABLE IF NOT EXISTS
	public.account_type (
		id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
		name TEXT NOT NULL,
		created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
		updated_at TIMESTAMP WITH TIME ZONE NULL
	);

CREATE UNIQUE INDEX IF NOT EXISTS account_type_name_uniq_idx ON public.account_type(name);

INSERT INTO
	public.account_type (
		name
	)
VALUES
	('Asset'),
	('Equity'),
	('Expense'),
	('Liability'),
	('Revenue');

CREATE TABLE IF NOT EXISTS
    public.user (
        id INT GENERATED ALWAYS  AS IDENTITY PRIMARY KEY,
        email TEXT NOT NULL,
		password TEXT NOT NULL,
        name TEXT NOT NULL,
        status_id INT NOT NULL REFERENCES status(id) DEFAULT 2,
        created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
        updated_at TIMESTAMP WITH TIME ZONE NULL
    );

CREATE UNIQUE INDEX IF NOT EXISTS user_email_uniq_idx ON public.user(email);

CREATE TABLE IF NOT EXISTS
	public.permission (
		id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
		name TEXT NOT NULL,
		status_id INT NOT NULL REFERENCES status(id) DEFAULT 1,
		created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
		updated_at TIMESTAMP WITH TIME ZONE NULL
	);

CREATE UNIQUE INDEX IF NOT EXISTS permission_name_uniq_idx ON public.permission(name);

CREATE TABLE IF NOT EXISTS
	public.budget (
		id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
		name TEXT NOT NULL,
		description TEXT NULL,
		status_id INT NOT NULL REFERENCES status(id) DEFAULT 1,
		created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
		updated_at TIMESTAMP WITH TIME ZONE NULL
	);

CREATE TABLE IF NOT EXISTS
	public.account (
		id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
		account_type_id INT NOT NULL REFERENCES account_type(id),
		budget_id INT NOT NULL REFERENCES budget(id),
		name TEXT NOT NULL,
		description TEXT NULL,
		currency_code TEXT NOT NULL,
		status_id INT NOT NULL REFERENCES status(id) DEFAULT 1,
		created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
		updated_at TIMESTAMP WITH TIME ZONE NULL
	);
