-- Servizio Interessi Table
CREATE TABLE IF NOT EXISTS public.servizio_interessi (
    "annuncioId" integer NOT NULL,
    "userId" UUID NOT NULL
);
-- Unique Constraint
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 
    FROM pg_constraint 
    WHERE conname = 'unique_interest'
    ) THEN
        ALTER TABLE public.servizio_interessi 
        ADD CONSTRAINT unique_interest UNIQUE ("annuncioId", "userId");
    END IF;
END $$;
-- Foreign Key to Annunci
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'interesse_annuncio'
    ) THEN
        ALTER TABLE public.servizio_interessi 
        ADD CONSTRAINT interesse_annuncio 
        FOREIGN KEY ("annuncioId") REFERENCES public.annunci (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;
-- Foreign Key to Users
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'interesse_user'
    ) THEN
        ALTER TABLE public.servizio_interessi 
        ADD CONSTRAINT interesse_user 
        FOREIGN KEY ("userId") REFERENCES public.users (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;