-- Potenziali table
CREATE TABLE IF NOT EXISTS public.potenziali (
    id UUID DEFAULT gen_random_uuid () NOT NULL,
    name TEXT NOT NULL,
    descrizione TEXT,
    created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
    userid UUID,
    potenziali_group_id UUID NOT NULL
);

-- Primary Key
DO $$ 
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'potenziali_pkey' 
        AND conrelid = 'public.potenziali'::regclass
    ) THEN
        ALTER TABLE public.potenziali
        ADD CONSTRAINT potenziali_pkey PRIMARY KEY (id);
    END IF;
END $$;

-- Foreign Key to Users
DO $$ 
BEGIN 
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint 
        WHERE conname = 'potenziali_assigned_user'
        AND conrelid = 'public.potenziali'::regclass
    ) THEN
        ALTER TABLE public.potenziali 
        ADD CONSTRAINT potenziali_assigned_user 
        FOREIGN KEY (userid) REFERENCES public.users (id) 
        ON UPDATE CASCADE ON DELETE SET NULL;
    END IF;
END $$;