-- For UUIDS
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE IF NOT EXISTS users (
    id BIGINT PRIMARY KEY,
    discord_id BIGINT,
    superuser BOOL NOT NULL DEFAULT false
);

CREATE TABLE IF NOT EXISTS orgs (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    name VARCHAR(32) UNIQUE NOT NULL,
    owner_id BIGINT REFERENCES users(id),
    discord_server_id BIGINT,
    active BOOLEAN NOT NULL DEFAULT true
);

CREATE TABLE IF NOT EXISTS org_managers (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    user_id BIGINT REFERENCES users(id) ON DELETE CASCADE,
    org_id UUID REFERENCES orgs(id) ON DELETE CASCADE,
    active BOOLEAN NOT NULL DEFAULT true
);

CREATE TABLE IF NOT EXISTS org_tags (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    tag VARCHAR(32) NOT NULL,
    org_id UUID REFERENCES orgs(id) ON DELETE CASCADE,
    UNIQUE (tag, org_id) -- A tag can only appear in a organization once
);

CREATE TABLE IF NOT EXISTS org_groups (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    name VARCHAR(32) NOT NULL,
    org_id UUID REFERENCES orgs(id) ON DELETE CASCADE,
    active BOOLEAN NOT NULL DEFAULT true,
    UNIQUE (name, org_id) -- A group may only have one name
);

CREATE TABLE IF NOT EXISTS org_group_tags (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    group_id UUID REFERENCES org_groups(id) ON DELETE CASCADE,
    tag_id UUID REFERENCES org_tags(id) ON DELETE CASCADE,
    UNIQUE (tag_id, group_id) -- A tag can only appear in a group once
);

CREATE TABLE IF NOT EXISTS org_group_notes (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    group_id UUID REFERENCES org_groups(id) ON DELETE CASCADE,
    note VARCHAR(512) NOT NULL
);

CREATE TABLE IF NOT EXISTS org_group_members (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    user_id BIGINT REFERENCES users(id) ON DELETE CASCADE,
    group_id UUID REFERENCES org_groups(id) ON DELETE CASCADE,
    owner BOOLEAN NOT NULL DEFAULT false,
    UNIQUE (user_id, group_id) -- A user can only appear in a group once
);

CREATE TABLE IF NOT EXISTS org_group_squad_details (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    group_id UUID REFERENCES org_groups(id) ON DELETE CASCADE,
    remote_admin_list VARCHAR(256),
    remote_admin_list_group VARCHAR(32),
    perm_changemap       BOOLEAN NOT NULL DEFAULT false,
    perm_pause           BOOLEAN NOT NULL DEFAULT false,
    perm_cheat           BOOLEAN NOT NULL DEFAULT false,
    perm_private         BOOLEAN NOT NULL DEFAULT false,
    perm_balance         BOOLEAN NOT NULL DEFAULT false,
    perm_chat            BOOLEAN NOT NULL DEFAULT false,
    perm_kick            BOOLEAN NOT NULL DEFAULT false,
    perm_ban             BOOLEAN NOT NULL DEFAULT false,
    perm_config          BOOLEAN NOT NULL DEFAULT false,
    perm_cameraman       BOOLEAN NOT NULL DEFAULT false,
    perm_immune          BOOLEAN NOT NULL DEFAULT false,
    perm_manageserver    BOOLEAN NOT NULL DEFAULT false,
    perm_featuretest     BOOLEAN NOT NULL DEFAULT false,
    perm_reserve         BOOLEAN NOT NULL DEFAULT false,
    perm_demos           BOOLEAN NOT NULL DEFAULT false,
    perm_clientdemos     BOOLEAN NOT NULL DEFAULT false,
    perm_debug           BOOLEAN NOT NULL DEFAULT false,
    perm_teamchange      BOOLEAN NOT NULL DEFAULT false,
    perm_forceteamchange BOOLEAN NOT NULL DEFAULT false,
    perm_canseeadminchat BOOLEAN NOT NULL DEFAULT false
);
