-- Add allowed client type for user in the game

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_type t
        INNER JOIN pg_namespace p ON t.typnamespace=p.oid
        WHERE t.typname='client_types' AND p.nspname='auth'
    ) THEN
        CREATE TYPE auth.client_types AS ENUM ('p', 'o', 'm');
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_class c
        INNER JOIN pg_namespace n ON n.oid = c.relnamespace
        INNER JOIN pg_attribute a ON a.attrelid = c.oid
        WHERE c.relname = 'players' AND n.nspname = 'games' AND a.attname = 'client_type'
    ) THEN
        ALTER TABLE games.players ADD COLUMN client_type auth.client_types NOT NULL DEFAULT 'p';
    END IF;
END
$$;