CREATE TABLE IF NOT EXISTS
    public.status (
        id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
        name VARCHAR(255) 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.user (
        id INT GENERATED ALWAYS  AS IDENTITY PRIMARY KEY,
        username VARCHAR(255) NOT NULL,
		password VARCHAR(255) NOT NULL,
        email VARCHAR(255) NOT NULL,
        name VARCHAR(255) 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_username_uniq_idx ON public.user(username);
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 VARCHAR(255) 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.user_permission (
		id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
		permission_id INT NOT NULL REFERENCES permission(id),
		user_id INT NOT NULL REFERENCES public.user(id),
		status_id INT NOT NULL REFERENCES status(id)
	);
