-- Migration to add Gitea repositories and update existing projects

-- Add provider field to projects table to distinguish between GitHub and Gitea
ALTER TABLE projects ADD COLUMN provider VARCHAR(50) DEFAULT 'github';
ALTER TABLE projects ADD COLUMN provider_base_url VARCHAR(255);
ALTER TABLE projects ADD COLUMN ssh_port INTEGER DEFAULT 22;

-- Add Gitea-specific configuration
ALTER TABLE projects ADD COLUMN gitea_enabled BOOLEAN DEFAULT false;
ALTER TABLE projects ADD COLUMN webhook_secret VARCHAR(255);
ALTER TABLE projects ADD COLUMN auto_assignment BOOLEAN DEFAULT true;

-- Update existing projects to mark them as GitHub
UPDATE projects SET provider = 'github', provider_base_url = 'https://github.com' WHERE provider IS NULL;

-- Add Gitea repositories
INSERT INTO projects (
    name, 
    description, 
    status, 
    github_repo, 
    git_url, 
    git_owner, 
    git_repository, 
    git_branch, 
    bzzz_enabled, 
    ready_to_claim, 
    private_repo, 
    github_token_required,
    provider,
    provider_base_url,
    ssh_port,
    gitea_enabled,
    auto_assignment
) VALUES 
(
    'whoosh-gitea', 
    'Distributed task coordination system with AI agents (Gitea)', 
    'active', 
    'tony/whoosh', 
    'ssh://git@192.168.1.113:2222/tony/whoosh.git', 
    'tony', 
    'whoosh', 
    'master', 
    true, 
    true, 
    false, 
    false,
    'gitea',
    'http://192.168.1.113:3000',
    2222,
    true,
    true
),
(
    'bzzz-gitea', 
    'P2P collaborative development coordination system (Gitea)', 
    'active', 
    'tony/bzzz', 
    'ssh://git@192.168.1.113:2222/tony/bzzz.git', 
    'tony', 
    'bzzz', 
    'main', 
    true, 
    true, 
    false, 
    false,
    'gitea',
    'http://192.168.1.113:3000',
    2222,
    true,
    true
);

-- Create repository_config table for provider-specific configuration
CREATE TABLE repository_config (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id INTEGER REFERENCES projects(id) ON DELETE CASCADE,
    provider VARCHAR(50) NOT NULL,
    config_data JSONB NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- Insert default Gitea configuration for our repositories
INSERT INTO repository_config (project_id, provider, config_data) 
SELECT 
    p.id,
    'gitea',
    jsonb_build_object(
        'base_url', p.provider_base_url,
        'owner', p.git_owner,
        'repository', p.git_repository,
        'task_label', 'bzzz-task',
        'in_progress_label', 'in-progress',
        'completed_label', 'completed',
        'base_branch', p.git_branch,
        'branch_prefix', 'bzzz/task-',
        'auto_assignment', p.auto_assignment,
        'ssh_port', p.ssh_port
    )
FROM projects p 
WHERE p.provider = 'gitea';

-- Create task assignment log table
CREATE TABLE task_assignments (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id INTEGER REFERENCES projects(id),
    task_number INTEGER NOT NULL,
    agent_id VARCHAR(255) NOT NULL,
    agent_role VARCHAR(255),
    assignment_reason TEXT,
    status VARCHAR(50) DEFAULT 'assigned', -- assigned, in_progress, completed, failed
    assigned_at TIMESTAMP DEFAULT NOW(),
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    results JSONB,
    error_message TEXT
);

-- Create indexes for task assignments
CREATE INDEX idx_task_assignments_project ON task_assignments(project_id);
CREATE INDEX idx_task_assignments_agent ON task_assignments(agent_id);
CREATE INDEX idx_task_assignments_status ON task_assignments(status);
CREATE INDEX idx_task_assignments_task ON task_assignments(project_id, task_number);

-- Create webhook events table for tracking repository events
CREATE TABLE webhook_events (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id INTEGER REFERENCES projects(id),
    event_type VARCHAR(100) NOT NULL,
    payload JSONB NOT NULL,
    processed BOOLEAN DEFAULT false,
    processed_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Create indexes for webhook events
CREATE INDEX idx_webhook_events_project ON webhook_events(project_id);
CREATE INDEX idx_webhook_events_type ON webhook_events(event_type);
CREATE INDEX idx_webhook_events_processed ON webhook_events(processed);
CREATE INDEX idx_webhook_events_created ON webhook_events(created_at);