-- Create types
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'question_type') THEN
CREATE TYPE question_type AS ENUM (
            'variant',
            'images',
            'varimg',
            'emoji',
            'text',
            'select',
            'date',
            'number',
            'file',
            'page',
            'rating'
        );
END IF;

    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'quiz_status') THEN
CREATE TYPE quiz_status AS ENUM (
            'draft',
            'template',
            'stop',
            'start',
            'timeout',
            'offlimit'
        );
END IF;

    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
END$$;

-- Create tables
CREATE TABLE IF NOT EXISTS account (
    id UUID PRIMARY KEY,
    user_id VARCHAR(24),
    email VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted BOOLEAN DEFAULT false
    );

CREATE TABLE IF NOT EXISTS quiz (
    id bigserial UNIQUE NOT NULL PRIMARY KEY,
    qid  uuid DEFAULT uuid_generate_v4(),
    accountid varchar(30) NOT NULL,
    deleted boolean DEFAULT false,
    archived boolean DEFAULT false,
    fingerprinting boolean DEFAULT false,
    repeatable boolean DEFAULT false,
    note_prevented boolean DEFAULT false,
    mail_notifications boolean DEFAULT false,
    unique_answers boolean DEFAULT false,
    super boolean DEFAULT false,
    group_id bigint DEFAULT 0,
    name varchar(280),
    description text,
    config text,
    status quiz_status DEFAULT 'draft',
    limit_answers integer DEFAULT 0,
    due_to integer DEFAULT 0,
    time_of_passing integer DEFAULT 0,
    pausable boolean DEFAULT false,
    version smallint DEFAULT 0,
    version_comment text DEFAULT '',
    parent_ids integer[],
    created_at timestamp DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
    questions_count integer DEFAULT 0,
    answers_count integer DEFAULT 0,
    average_time_passing integer DEFAULT 0
    );

CREATE TABLE IF NOT EXISTS question (
    id bigserial UNIQUE NOT NULL PRIMARY KEY,
    quiz_id bigint NOT NULL,
    title varchar(512) NOT NULL,
    description text,
    questiontype question_type DEFAULT 'text',
    required boolean DEFAULT false,
    deleted boolean DEFAULT false,
    page smallint DEFAULT 0,
    content text,
    version smallint DEFAULT 0,
    parent_ids integer[],
    created_at timestamp DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT quiz_relation FOREIGN KEY(quiz_id) REFERENCES quiz(id)
    );

CREATE TABLE IF NOT EXISTS answer (
    id bigserial UNIQUE NOT NULL PRIMARY KEY,
    content text,
    quiz_id bigint NOT NULL REFERENCES quiz(id),
    question_id bigint NOT NULL REFERENCES question(id),
    fingerprint varchar(1024),
    session varchar(20),
    created_at timestamp DEFAULT CURRENT_TIMESTAMP
    );

CREATE TABLE IF NOT EXISTS privileges (
    id SERIAL PRIMARY KEY,
    privilegeID VARCHAR(50),
    account_id UUID,
    privilege_name VARCHAR(255),
    amount INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (account_id) REFERENCES account (id)
    );

-- Create indexes
CREATE INDEX IF NOT EXISTS active ON question(deleted) WHERE deleted=false;
CREATE INDEX IF NOT EXISTS relation ON question(quiz_id DESC);
CREATE INDEX IF NOT EXISTS required ON question(required DESC);
CREATE INDEX IF NOT EXISTS questiontype ON question(questiontype);
CREATE INDEX IF NOT EXISTS active ON quiz(deleted, archived, status) WHERE deleted = false AND archived = false AND status = 'start';
CREATE INDEX IF NOT EXISTS timeouted ON quiz(due_to DESC) WHERE deleted = false AND due_to <> 0 AND status <> 'timeout';
CREATE INDEX IF NOT EXISTS groups ON quiz(super) WHERE super = true;
CREATE INDEX IF NOT EXISTS birthtime ON quiz(created_at DESC);
CREATE INDEX IF NOT EXISTS subquizes ON quiz(group_id DESC) WHERE group_id <> 0;
