-- Banlist Type Enum
DO $$ BEGIN
    CREATE TYPE public."BanType" AS ENUM ('ip', 'email', 'phone', 'cf');

EXCEPTION WHEN duplicate_object THEN NULL;
END $$;
-- Banlist Table
CREATE TABLE IF NOT EXISTS public.banlist (
    id integer NOT NULL,
    VALUE TEXT NOT NULL,
    type public."BanType" NOT NULL
);
-- Sequence for Banlist ID
DO $$ BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_sequences 
        WHERE schemaname = 'public' AND sequencename = 'banlist_id_seq'
    ) THEN
        CREATE SEQUENCE public.banlist_id_seq 
        AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
    END IF;
END $$;

ALTER SEQUENCE public.banlist_id_seq OWNED BY public.banlist.id;
-- Set default for Banlist ID
ALTER TABLE public.banlist
ALTER COLUMN id
SET DEFAULT nextval(
    'public.banlist_id_seq'::regclass
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.banlist 
ADD CONSTRAINT banlist_pkey PRIMARY KEY (id);
    EXCEPTION
        WHEN invalid_table_definition THEN
            RAISE NOTICE 'Primary key already exists. Ignoring...'; 
END $$;
-- Unique Constraint on type and VALUE
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 
    FROM pg_constraint 
    WHERE conname = 'banlist_typeval'
    ) THEN
        ALTER TABLE public.banlist 
        ADD CONSTRAINT banlist_typeval UNIQUE (type, VALUE);
    END IF;
END $$;
-- Banners Table
CREATE TABLE IF NOT EXISTS public.banners (
    idbanner TEXT NOT NULL,
    titolo TEXT,
    testo TEXT,
    is_unskippable boolean NOT NULL,
    has_cta boolean NOT NULL,
    cta_href TEXT,
    cta_icon TEXT,
    cta_text TEXT,
    color TEXT,
    is_active boolean NOT NULL,
    show_public boolean NOT NULL,
    show_private boolean NOT NULL,
    hide_duration integer
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.banners 
ADD CONSTRAINT banners_pkey PRIMARY KEY (idbanner);
    EXCEPTION   
        WHEN invalid_table_definition THEN 
            RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Event Queue Table
CREATE TABLE IF NOT EXISTS public.event_queue (
    event_id integer NOT NULL,
    lock_expires TIMESTAMP WITH TIME ZONE NOT NULL,
    DATA jsonb,
    status TEXT DEFAULT 'pending'::TEXT NOT NULL
);
-- Sequence for event_id
DO $$ BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_sequences 
        WHERE schemaname = 'public' AND sequencename = 'event_queue_event_id_seq'
    ) THEN
        CREATE SEQUENCE public.event_queue_event_id_seq 
        AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
    END IF;
END $$;

ALTER SEQUENCE public.event_queue_event_id_seq OWNED BY public.event_queue.event_id;
-- Set default for event_id
ALTER TABLE public.event_queue
ALTER COLUMN event_id
SET DEFAULT nextval(
    'public.event_queue_event_id_seq'::regclass
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.event_queue 
ADD CONSTRAINT event_queue_pkey PRIMARY KEY (event_id);
    EXCEPTION   
        WHEN invalid_table_definition THEN 
            RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Flags Table
CREATE TABLE IF NOT EXISTS public.flags (
    id TEXT NOT NULL,
    VALUE TEXT NOT NULL
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.flags 
ADD CONSTRAINT sitesettings_pkey PRIMARY KEY (id);
    EXCEPTION   
        WHEN invalid_table_definition THEN 
            RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Testi e Stringhe Table
CREATE TABLE IF NOT EXISTS public.testi_e_stringhe (
    stinga_id TEXT NOT NULL,
    stringa_value TEXT
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.testi_e_stringhe 
ADD CONSTRAINT testi_e_stringhe_pkey PRIMARY KEY (stinga_id);
    EXCEPTION   
        WHEN invalid_table_definition THEN 
            RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Rate Limiter Table
CREATE TABLE IF NOT EXISTS public.ratelimiter (
    KEY TEXT NOT NULL,
    request_timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    request_count integer NOT NULL
);
-- Index on KEY
CREATE INDEX IF NOT EXISTS ratelimiter_key_index ON public.ratelimiter USING btree (KEY);