CREATE TABLE IF NOT EXISTS public.storage (
    id uuid NOT NULL DEFAULT uuidv7(),
    created_at timestamp without time zone NOT NULL DEFAULT now(),
    filename text NOT NULL,
    mime_type text NOT NULL,
    file_size_bytes INT NOT NULL,
    file_data BYTEA NOT NULL,
    expires_at timestamp without time zone,
    tag text
);


DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'storage'
        AND column_name = 'file_data'
) THEN
ALTER TABLE public.storage
    ALTER COLUMN file_data SET STORAGE EXTERNAL;
END IF;
END $$;

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

--- users_storage
DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'users_storage'
) THEN
TRUNCATE TABLE users_storage CASCADE;
END IF;
END $$;

ALTER TABLE IF EXISTS public.users_storage
DROP COLUMN IF EXISTS  "storageId";

ALTER TABLE IF EXISTS public.users_storage
    ADD COLUMN IF NOT EXISTS storage_id uuid;

DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'userstorage_storage'
    AND conrelid = 'public.users_storage'::regclass
    ) THEN
        ALTER TABLE public.users_storage 
        ADD CONSTRAINT userstorage_storage 
        FOREIGN KEY (storage_id) REFERENCES public.storage (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;

ALTER TABLE users_storage 
DROP CONSTRAINT IF EXISTS users_storage_user_storage_key;


DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'users_storage_user_storage_key'
    ) THEN
        ALTER TABLE public.users_storage 
        ADD CONSTRAINT users_storage_user_storage_key UNIQUE ("userId", storage_id);
    END IF;
END $$;

---IMAGES

DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'images_refs'
) THEN
TRUNCATE TABLE images_refs;
END IF;
END $$;

DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'images_refs'
    AND column_name = 'img'
) THEN
ALTER TABLE images_refs
ALTER COLUMN img TYPE uuid USING img::uuid;
END IF;
END $$;

DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'images_refs_img_storage'
    AND conrelid = 'public.images_refs'::regclass
    ) THEN
        ALTER TABLE public.images_refs 
        ADD CONSTRAINT images_refs_img_storage 
        FOREIGN KEY (img) REFERENCES public.storage (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;

DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'images_refs'
    AND column_name = 'thumb'
) THEN
ALTER TABLE images_refs
ALTER COLUMN thumb TYPE uuid USING thumb::uuid;
END IF;
END $$;

DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'images_refs_thumb_storage'
    AND conrelid = 'public.images_refs'::regclass
    ) THEN
        ALTER TABLE public.images_refs 
        ADD CONSTRAINT images_refs_thumb_storage 
        FOREIGN KEY (thumb) REFERENCES public.storage (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;

--- VIDEOS

DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'videos_refs'
) THEN
TRUNCATE TABLE videos_refs;
END IF;
END $$;


DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'videos_refs'
    AND column_name = 'video'
) THEN
ALTER TABLE videos_refs
ALTER COLUMN video TYPE uuid USING video::uuid;
END IF;
END $$;

DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'videos_refs_video_storage'
    AND conrelid = 'public.videos_refs'::regclass
    ) THEN
        ALTER TABLE public.videos_refs 
        ADD CONSTRAINT videos_refs_video_storage 
        FOREIGN KEY (video) REFERENCES public.storage (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;

DO $$ BEGIN IF EXISTS (
    SELECT
    FROM information_schema.columns
    WHERE table_name = 'videos_refs'
    AND column_name = 'thumb'
) THEN
ALTER TABLE videos_refs
ALTER COLUMN thumb TYPE uuid USING thumb::uuid;
END IF;
END $$;

DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'videos_refs_thumb_storage'
    AND conrelid = 'public.videos_refs'::regclass
    ) THEN
        ALTER TABLE public.videos_refs 
        ADD CONSTRAINT videos_refs_thumb_storage 
        FOREIGN KEY (thumb) REFERENCES public.storage (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;

