-- Tipologia Enum
DO $$ BEGIN
CREATE TYPE public."TipologiaPosizioneEnum" AS ENUM ('Transitorio', 'Stabile');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;
-- Servizio Table
CREATE TABLE IF NOT EXISTS public.servizio (
    user_id UUID NOT NULL,
    created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
    tipologia public."TipologiaPosizioneEnum" NOT NULL,
    arredato boolean DEFAULT FALSE NOT NULL,
    budget integer NOT NULL,
    n_adulti integer NOT NULL,
    n_minori integer NOT NULL,
    animali boolean DEFAULT FALSE NOT NULL,
    fumatori boolean DEFAULT FALSE NOT NULL,
    giardino boolean DEFAULT FALSE NOT NULL,
    parcheggio boolean DEFAULT FALSE NOT NULL,
    terrazzo boolean DEFAULT FALSE NOT NULL,
    ascensore boolean DEFAULT FALSE NOT NULL,
    pianoterra boolean DEFAULT FALSE NOT NULL,
    motivazione_transitorio TEXT,
    reddito TEXT,
    "isSent" boolean DEFAULT FALSE NOT NULL,
    servizio_id UUID DEFAULT gen_random_uuid () NOT NULL,
    decorrenza TIMESTAMP WITHOUT TIME ZONE,
    "isInterrotto" boolean DEFAULT FALSE NOT NULL,
    entromese integer,
    permanenza integer,
    "isOkAcconto" boolean DEFAULT FALSE NOT NULL,
    "isOkSaldo" boolean DEFAULT FALSE NOT NULL,
    "isOkConsulenza" boolean DEFAULT FALSE NOT NULL,
    -- doc_personale_fronte_ref UUID,
    -- doc_personale_retro_ref UUID,
    doc_motivazione_ref UUID
);
-- Primary key
DO $$ BEGIN
ALTER TABLE public.servizio 
ADD CONSTRAINT servizio_pkey PRIMARY KEY (servizio_id);
    EXCEPTION
        WHEN invalid_table_definition THEN
            RAISE NOTICE 'Primary key already exists. Ignoring...'; 
END $$;
-- Foreign Key to Users
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'user_servizio'
    ) THEN
        ALTER TABLE public.servizio 
        ADD CONSTRAINT user_servizio 
        FOREIGN KEY (user_id) REFERENCES public.users (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;
-- Foreign Key to User Storage
-- DO $$ BEGIN IF NOT EXISTS (
--     SELECT 1 FROM pg_constraint
--     WHERE conname = 'servizio_doc_fronte'
--     ) THEN
--         ALTER TABLE public.servizio
--         ADD CONSTRAINT servizio_doc_fronte
--         FOREIGN KEY (doc_personale_fronte_ref) REFERENCES public.users_storage (user_storage_id)
--         ON UPDATE CASCADE ON DELETE SET NULL;
--     END IF;
-- END $$;
-- -- Foreign Key to User Storage
-- DO $$ BEGIN IF NOT EXISTS (
--     SELECT 1 FROM pg_constraint
--     WHERE conname = 'servizio_doc_retro'
--     ) THEN
--         ALTER TABLE public.servizio
--         ADD CONSTRAINT servizio_doc_retro
--         FOREIGN KEY (doc_personale_retro_ref) REFERENCES public.users_storage (user_storage_id)
--         ON UPDATE CASCADE ON DELETE SET NULL;
--     END IF;
-- END $$;
-- Foreign Key to User Storage
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'servizio_doc_motivazione'
    ) THEN
        ALTER TABLE public.servizio 
        ADD CONSTRAINT servizio_doc_motivazione 
        FOREIGN KEY (doc_motivazione_ref) REFERENCES public.users_storage (user_storage_id) 
        ON UPDATE CASCADE ON DELETE SET NULL;
    END IF;
END $$;