-- Initial schema for WHOOSH MVP
-- Minimal subset focused on single-agent execution mode

-- Teams table - core team management
CREATE TABLE teams (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    description TEXT,
    status VARCHAR(50) NOT NULL DEFAULT 'forming',
    task_id UUID,
    gitea_issue_url TEXT,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    completed_at TIMESTAMP WITH TIME ZONE
);

-- Team roles enumeration
CREATE TABLE team_roles (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    capabilities JSONB,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Team assignments - who's on what team
CREATE TABLE team_assignments (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    team_id UUID NOT NULL REFERENCES teams(id) ON DELETE CASCADE,
    agent_id UUID NOT NULL,
    role_id INTEGER NOT NULL REFERENCES team_roles(id),
    status VARCHAR(50) NOT NULL DEFAULT 'active',
    assigned_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    completed_at TIMESTAMP WITH TIME ZONE,
    UNIQUE(team_id, agent_id)
);

-- Minimal agents registry
CREATE TABLE agents (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    endpoint_url TEXT NOT NULL,
    capabilities JSONB NOT NULL DEFAULT '{}',
    status VARCHAR(50) NOT NULL DEFAULT 'available',
    last_seen TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    performance_metrics JSONB DEFAULT '{}',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Minimal SLURP submissions tracking
CREATE TABLE slurp_submissions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    team_id UUID REFERENCES teams(id) ON DELETE CASCADE,
    ucxl_address TEXT NOT NULL UNIQUE,
    artifact_type VARCHAR(100) NOT NULL,
    metadata JSONB DEFAULT '{}',
    submitted_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    status VARCHAR(50) NOT NULL DEFAULT 'submitted'
);

-- Insert default team roles for MVP
INSERT INTO team_roles (name, description, capabilities) VALUES 
('executor', 'Single-agent task executor', '{"code_generation": true, "task_execution": true, "git_operations": true}'),
('coordinator', 'Team coordination and oversight', '{"team_management": true, "task_planning": true, "quality_assurance": true}'),
('reviewer', 'Code and output review', '{"code_review": true, "quality_assurance": true, "documentation": true}');

-- Indexes for performance
CREATE INDEX idx_teams_status ON teams(status);
CREATE INDEX idx_teams_created_at ON teams(created_at);
CREATE INDEX idx_team_assignments_team_id ON team_assignments(team_id);
CREATE INDEX idx_team_assignments_agent_id ON team_assignments(agent_id);
CREATE INDEX idx_agents_status ON agents(status);
CREATE INDEX idx_agents_last_seen ON agents(last_seen);
CREATE INDEX idx_slurp_submissions_team_id ON slurp_submissions(team_id);
CREATE INDEX idx_slurp_submissions_ucxl_address ON slurp_submissions(ucxl_address);