-- GOXY Database Migration for Supabase
-- Run this in Supabase SQL Editor: https://supabase.com/dashboard/project/_/sql

-- Step 1: Create ENUM types
CREATE TYPE task_type_enum AS ENUM (
    'general', 'greeting', 'faq', 'support', 'creative'
);

CREATE TYPE toxicity_level_enum AS ENUM (
    'none', 'low', 'medium', 'high'
);

CREATE TYPE feedback_type_enum AS ENUM (
    'good', 'bad', 'report'
);

-- Step 2: Create responses table
CREATE TABLE responses (
    id UUID PRIMARY KEY,
    input_message TEXT NOT NULL,
    input_context TEXT,
    task_type task_type_enum NOT NULL,
    max_length INTEGER NOT NULL,
    temperature FLOAT NOT NULL,
    top_p FLOAT NOT NULL,
    generated_text TEXT NOT NULL,
    quality_score FLOAT NOT NULL,
    toxicity_score FLOAT NOT NULL,
    confidence FLOAT NOT NULL,
    toxicity_level toxicity_level_enum NOT NULL,
    generation_time_ms FLOAT NOT NULL,
    tokens_generated INTEGER NOT NULL,
    model_name VARCHAR(100),
    model_version VARCHAR(50),
    user_id VARCHAR(100),
    is_approved BOOLEAN,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);

-- Step 3: Create feedbacks table
CREATE TABLE feedbacks (
    id UUID PRIMARY KEY,
    response_id UUID NOT NULL,
    feedback_type feedback_type_enum NOT NULL,
    comment TEXT,
    user_id VARCHAR(100),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    FOREIGN KEY (response_id) REFERENCES responses(id) ON DELETE CASCADE
);

-- Step 4: Create indexes for responses
CREATE INDEX idx_responses_id ON responses(id);
CREATE INDEX idx_responses_quality_score ON responses(quality_score);
CREATE INDEX idx_responses_toxicity_score ON responses(toxicity_score);
CREATE INDEX idx_responses_toxicity_level ON responses(toxicity_level);
CREATE INDEX idx_responses_created_at ON responses(created_at);
CREATE INDEX idx_responses_user_id ON responses(user_id);
CREATE INDEX idx_responses_created_at_desc ON responses(created_at DESC);
CREATE INDEX idx_responses_toxicity_quality ON responses(toxicity_score, quality_score);
CREATE INDEX idx_responses_task_type_created ON responses(task_type, created_at);

-- Step 5: Create indexes for feedbacks
CREATE INDEX idx_feedbacks_id ON feedbacks(id);
CREATE INDEX idx_feedbacks_response_id ON feedbacks(response_id);
CREATE INDEX idx_feedbacks_feedback_type ON feedbacks(feedback_type);
CREATE INDEX idx_feedbacks_user_id ON feedbacks(user_id);
CREATE INDEX idx_feedbacks_created_at ON feedbacks(created_at);
CREATE INDEX idx_feedbacks_response_created ON feedbacks(response_id, created_at);
CREATE INDEX idx_feedbacks_type_created ON feedbacks(feedback_type, created_at);

-- Done! Tables created successfully.
