-- Drop tables if they exist DROP TABLE IF EXISTS public.tools; DROP TABLE IF EXISTS public.users; -- Create the users table CREATE TABLE public.users ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, email TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL, created_at TIMESTAMPTZ DEFAULT now() ); -- Create the tools table CREATE TABLE IF NOT EXISTS public.tools ( id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, url TEXT NOT NULL, category TEXT NOT NULL, tags TEXT[] DEFAULT '{}', likes INTEGER DEFAULT 0, added_at BIGINT NOT NULL, image_url TEXT, i18n JSONB NOT NULL, user_id UUID REFERENCES public.users(id) ON DELETE CASCADE ); -- Create the games table CREATE TABLE IF NOT EXISTS public.games ( id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, url TEXT NOT NULL, category TEXT NOT NULL DEFAULT 'game', tags TEXT[] DEFAULT '{}', likes INTEGER DEFAULT 0, added_at BIGINT NOT NULL, image_url TEXT, i18n JSONB NOT NULL, user_id UUID REFERENCES public.users(id) ON DELETE CASCADE ); -- Create the prompts table CREATE TABLE IF NOT EXISTS public.prompts ( id TEXT PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, url TEXT NOT NULL, category TEXT NOT NULL DEFAULT 'prompt', tags TEXT[] DEFAULT '{}', likes INTEGER DEFAULT 0, added_at BIGINT NOT NULL, image_url TEXT, i18n JSONB NOT NULL, user_id UUID REFERENCES public.users(id) ON DELETE CASCADE ); -- Dynamics table CREATE TABLE IF NOT EXISTS public.dynamics ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, title TEXT NOT NULL, content TEXT NOT NULL, url TEXT, source TEXT, category TEXT, published_at TIMESTAMPTZ DEFAULT now() ); -- Performance Indexes for Search CREATE INDEX IF NOT EXISTS idx_tools_name_description ON public.tools USING gin(to_tsvector('english', name || ' ' || description)); CREATE INDEX IF NOT EXISTS idx_games_name_description ON public.games USING gin(to_tsvector('english', name || ' ' || description)); CREATE INDEX IF NOT EXISTS idx_prompts_name_description ON public.prompts USING gin(to_tsvector('english', name || ' ' || description)); -- Ratings table CREATE TABLE IF NOT EXISTS public.ratings ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, item_id TEXT NOT NULL, item_type TEXT NOT NULL CHECK (item_type IN ('tool', 'game', 'prompt')), user_id UUID REFERENCES public.users(id) ON DELETE CASCADE, rating INTEGER NOT NULL CHECK (rating >= 1 AND rating <= 5), created_at TIMESTAMPTZ DEFAULT now(), UNIQUE(item_id, item_type, user_id) ); -- Comments table CREATE TABLE IF NOT EXISTS public.comments ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, item_id TEXT NOT NULL, item_type TEXT NOT NULL CHECK (item_type IN ('tool', 'game', 'prompt')), user_id UUID REFERENCES public.users(id) ON DELETE CASCADE, content TEXT NOT NULL, created_at TIMESTAMPTZ DEFAULT now() ); -- Indexes for performance CREATE INDEX IF NOT EXISTS idx_ratings_item ON public.ratings(item_id, item_type); CREATE INDEX IF NOT EXISTS idx_comments_item ON public.comments(item_id, item_type);