-- Database schema for Cloud MCP Server
-- PostgreSQL database schema

-- Code Snippets table
CREATE TABLE IF NOT EXISTS code_snippets (
    id VARCHAR(255) PRIMARY KEY,
    title TEXT NOT NULL,
    code TEXT NOT NULL,
    language VARCHAR(100) NOT NULL,
    tags TEXT[] DEFAULT '{}',
    category VARCHAR(100),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Notes table
CREATE TABLE IF NOT EXISTS notes (
    id VARCHAR(255) PRIMARY KEY,
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    tags TEXT[] DEFAULT '{}',
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Tasks table
CREATE TABLE IF NOT EXISTS tasks (
    id VARCHAR(255) PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    completed BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    completed_at TIMESTAMP WITH TIME ZONE
);

-- Baby Milestones table
CREATE TABLE IF NOT EXISTS baby_milestones (
    id VARCHAR(255) PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT NOT NULL,
    date DATE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Math Resources table
CREATE TABLE IF NOT EXISTS math_resources (
    id VARCHAR(255) PRIMARY KEY,
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    grade VARCHAR(50),
    difficulty VARCHAR(50),
    tags TEXT[] DEFAULT '{}',
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Game Wishlist table
CREATE TABLE IF NOT EXISTS game_wishlist (
    id VARCHAR(255) PRIMARY KEY,
    game_name TEXT NOT NULL,
    platform VARCHAR(100),
    notes TEXT,
    added_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Create indexes for better search performance
CREATE INDEX IF NOT EXISTS idx_code_snippets_tags ON code_snippets USING GIN(tags);
CREATE INDEX IF NOT EXISTS idx_code_snippets_language ON code_snippets(language);
CREATE INDEX IF NOT EXISTS idx_code_snippets_category ON code_snippets(category);

CREATE INDEX IF NOT EXISTS idx_notes_tags ON notes USING GIN(tags);

CREATE INDEX IF NOT EXISTS idx_tasks_completed ON tasks(completed);
CREATE INDEX IF NOT EXISTS idx_tasks_created_at ON tasks(created_at);

CREATE INDEX IF NOT EXISTS idx_baby_milestones_date ON baby_milestones(date);

CREATE INDEX IF NOT EXISTS idx_math_resources_tags ON math_resources USING GIN(tags);
CREATE INDEX IF NOT EXISTS idx_math_resources_grade ON math_resources(grade);
CREATE INDEX IF NOT EXISTS idx_math_resources_difficulty ON math_resources(difficulty);

CREATE INDEX IF NOT EXISTS idx_game_wishlist_platform ON game_wishlist(platform);
