DELETE TYPE IF EXISTS "UserRole";

DELETE TYPE IF EXISTS "ActionType";

DROP TABLE IF EXISTS "actions";

DROP TABLE IF EXISTS "chatters";

DROP TABLE IF EXISTS "chatter_groups";

DROP TABLE IF EXISTS "redemptions";

DROP TABLE IF EXISTS "permissions";

DROP TABLE IF EXISTS "policies";

DROP TABLE IF EXISTS "groups";

DROP TABLE IF EXISTS "connections";

DROP TABLE IF EXISTS "accounts";

DROP TABLE IF EXISTS "api_keys";

DROP TABLE IF EXISTS "users";

CREATE TYPE "UserRole" AS ENUM ('USER', 'ADMIN');

CREATE TYPE "ActionType" AS ENUM (
    'WRITE_TO_FILE',
    'APPEND_TO_FILE',
    'AUDIO_FILE',
    'OBS_TRANSFORM',
    'RANDOM_TTS_VOICE',
    'SPECIFIC_TTS_VOICE',
    'TTS_MESSAGE',
    'TOGGLE_OBS_VISIBILITY',
    'SPECIFIC_OBS_VISIBILITY',
    'SPECIFIC_OBS_INDEX',
    'SLEEP',
    'OAUTH',
    'NIGHTBOT_PLAY',
    'NIGHTBOT_PAUSE',
    'NIGHTBOT_SKIP',
    'TWITCH_OAUTH',
    'NIGHTBOT_CLEAR_PLAYLIST',
    'NIGHTBOT_CLEAR_QUEUE',
    'VEADOTUBE_SET_STATE',
    'VEADOTUBE_PUSH_STATE',
    'VEADOTUBE_POP_STATE'
);

CREATE TABLE
    users (
        user_id uuid DEFAULT gen_random_uuid (),
        name text NOT NULL,
        email text,
        role "UserRole" NOT NULL DEFAULT USER,
        image text,
        tts_default_voice text NOT NULL,
        created_at timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
        updated_at timestamp(3) NOT NULL,
        CONSTRAINT "users_pkey" PRIMARY KEY ("user_id")
    );

CREATE TABLE
    accounts (
        account_id uuid DEFAULT gen_random_uuid (),
        user_id uuid NOT NULL,
        type text NOT NULL,
        provider text NOT NULL,
        providerAccountId text NOT NULL,
        access_token text NOT NULL,
        refresh_token text NOT NULL,
        expires_at integer NOT NULL,
        token_type text NOT NULL,
        scope text NOT NULL,
        created_at timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
        CONSTRAINT "accounts_pkey" PRIMARY KEY ("account_id"),
        CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id")
    );

CREATE TABLE
    actions (
        user_id uuid DEFAULT gen_random_uuid (),
        name text NOT NULL,
        type "ActionType" NOT NULL,
        data jsonb NOT NULL,
        has_message boolean NOT NULL DEFAULT false,
        CONSTRAINT "Action_pkey" PRIMARY KEY ("user_id", "name"),
        CONSTRAINT "s_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    api_keys (
        user_id uuid NOT NULL,
        key text NOT NULL,
        label text NOT NULL,
        CONSTRAINT "api_keys_pkey" PRIMARY KEY ("key") CONSTRAINT "api_keys_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    chatters (chatter_id text NOT NULL, name text NOT NULL);

CREATE TABLE
    chatter_groups (
        chatter_id text DEFAULT gen_random_uuid (),
        group_id uuid DEFAULT gen_random_uuid (),
        user_id uuid NOT NULL,
        chatter_label text NOT NULL,
        CONSTRAINT "chatter_groups_pkey" PRIMARY KEY ("user_id", "group_id", "chatter_id") CONSTRAINT "chatter_groups_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    connections (
        user_id uuid NOT NULL,
        name text NOT NULL,
        provider text NOT NULL,
        grant_type text NOT NULL,
        client_id text NOT NULL,
        access_token NOT NULL,
        scope text NOT NULL,
        expires_at timestamp(3) NOT NULL,
        default boolean NOT NULL DEFAULT false,
        CONSTRAINT "connections_pkey" PRIMARY KEY ("user_id", "name") CONSTRAINT "connections_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    connection_previews (
        user_id uuid NOT NULL,
        name text NOT NULL,
        provider text NOT NULL,
        grant_type text NOT NULL,
        client_id text NOT NULL,
        state text NOT NULL,
        CONSTRAINT "connection_previews_pkey" PRIMARY KEY ("user_id", "name") CONSTRAINT "connection_previews_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    emotes (
        emote_id text NOT NULL,
        label text NOT NULL,
        CONSTRAINT "emotes_pkey" PRIMARY KEY ("emote_id")
    );

CREATE TABLE
    emote_usages (
        emote_id text NOT NULL,
        chatter_id text NOT NULL,
        broadcaster_id text NOT NULL,
        timestamp timestamp(3) NOT NULL
    );

CREATE TABLE
    groups (
        group_id uuid DEFAULT gen_random_uuid (),
        user_id uuid NOT NULL,
        name text NOT NULL,
        priority integer NOT NULL,
        CONSTRAINT "groups_pkey" PRIMARY KEY ("group_id"),
        CONSTRAINT "groups_user_id_name_unique" UNIQUE ("user_id", "name") CONSTRAINT "groups_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE INDEX "groups_userId_idx" ON "groups" USING btree ("user_id");

CREATE TABLE
    permissions (
        permission_id uuid DEFAULT gen_random_uuid (),
        group_id uuid NOT NULL,
        user_id uuid NOT NULL,
        path text NOT NULL,
        allow boolean,
        CONSTRAINT "permissions" PRIMARY KEY ("permission_id") CONSTRAINT "permissions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE INDEX "permissions_user_id_idx" ON "permissions" USING btree ("user_id");

CREATE TABLE
    policies (
        policy_id uuid DEFAULT gen_random_uuid (),
        group_id uuid NOT NULL,
        user_id uuid NOT NULL,
        path text NOT NULL,
        count integer NOT NULL,
        span integer NOT NULL,
        CONSTRAINT "policies_pkey" PRIMARY KEY ("policy_id") CONSTRAINT "policies_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE INDEX "policies_user_id_idx" ON "policies" USING btree ("user_id");

CREATE TABLE
    impersonations (
        source_id uuid NOT NULL,
        target_id uuid NOT NULL,
        CONSTRAINT "impersonations_pkey" PRIMARY KEY ("source_id") CONSTRAINT "impersonations_source_id_fkey" FOREIGN KEY ("source_id") REFERENCES users ("user_id") ON DELETE CASCADE CONSTRAINT "impersonations_target_id_fkey" FOREIGN KEY ("target_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    redemption_actions (
        redemption_actions_id uuid DEFAULT gen_random_uuid (),
        user_id uuid NOT NULL,
        provider_redemption_id text,
        action_name text NOT NULL,
        order integer NOT NULL,
        state boolean NOT NULL,
        CONSTRAINT "redemption_actions_pkey" PRIMARY KEY ("redemption_actions_id"),
        CONSTRAINT "redemptions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    tts_chatter_voices (
        chatter_id text NOT NULL,
        user_id uuid NOT NULL,
        tts_voice_id uuid NOT NULL,
        CONSTRAINT "tts_chatter_voices" PRIMARY KEY ("user_id", "chatter_id") CONSTRAINT "tts_chatter_voices_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    tts_voices (
        tts_voice_id uuid DEFAULT gen_random_uuid (),
        name uuid NOT NULL,
        gender text,
        base_language text CONSTRAINT "tts_voices_pkey" PRIMARY KEY ("tts_voice_id"),
    );

CREATE TABLE
    tts_voice_states (
        tts_voice_id uuid NOT NULL,
        user_id NOT NULL,
        state boolean NOT NULL DEFAULT true,
        CONSTRAINT "tts_voice_states_pkey" PRIMARY KEY ("tts_voice_id", "user_id"),
        CONSTRAINT "tts_voice_states_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );

CREATE TABLE
    tts_word_filters (
        tts_word_filter_id uuid DEFAULT gen_random_uuid (),
        user_id NOT NULL,
        search text NOT NULL,
        replace text NOT NULL,
        flag integer NOT NULL,
        CONSTRAINT "tts_word_filters_pkey" PRIMARY KEY ("tts_word_filter_id"),
        CONSTRAINT "tts_word_filters_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES users ("user_id") ON DELETE CASCADE
    );