// =============================================================================
// Gitea tool registrations (in-process path used by agent-runner).
//
// All logic lives in ./gitea-api.ts so the MCP server (src/mcp/gitea-server.ts)
// and this in-process registry call the exact same code path. Keep this file
// purely about: (a) surface-shape for the LLM (name/description/parameters),
// (b) mapping ctx.gitea → GiteaConfig. No business logic here.
// =============================================================================

import { registerTool } from './registry';
import * as api from './gitea-api';

registerTool({
    name: 'gitea_create_issue',
    description: 'Create a new issue in a Gitea repository.',
    parameters: {
        type: 'object',
        properties: {
            repo: { type: 'string', description: 'Repository in "owner/name" format' },
            title: { type: 'string', description: 'Issue title' },
            body: { type: 'string', description: 'Issue body (markdown)' },
            labels: { type: 'array', items: { type: 'string' }, description: 'Optional label names' }
        },
        required: ['repo', 'title', 'body']
    },
    async handler(args, ctx) {
        return api.createIssue(ctx.gitea, {
            repo: String(args.repo),
            title: String(args.title),
            body: String(args.body),
            labels: Array.isArray(args.labels) ? (args.labels as string[]) : undefined,
        });
    }
});

registerTool({
    name: 'gitea_list_issues',
    description: 'List open issues in a Gitea repository.',
    parameters: {
        type: 'object',
        properties: {
            repo: { type: 'string', description: 'Repository in "owner/name" format' },
            state: { type: 'string', description: '"open", "closed", or "all". Default: "open"' }
        },
        required: ['repo']
    },
    async handler(args, ctx) {
        return api.listIssues(ctx.gitea, String(args.repo), String(args.state || 'open'));
    }
});

registerTool({
    name: 'gitea_close_issue',
    description: 'Close an issue in a Gitea repository.',
    parameters: {
        type: 'object',
        properties: {
            repo: { type: 'string', description: 'Repository in "owner/name" format' },
            issue_number: { type: 'number', description: 'Issue number to close' }
        },
        required: ['repo', 'issue_number']
    },
    async handler(args, ctx) {
        return api.closeIssue(ctx.gitea, String(args.repo), Number(args.issue_number));
    }
});

registerTool({
    name: 'list_repos',
    description: 'List all Git repositories in the Gitea organization. Returns repo names, descriptions, and last update time.',
    parameters: { type: 'object', properties: {} },
    async handler(_args, ctx) {
        return api.listRepos(ctx.gitea);
    }
});

registerTool({
    name: 'list_all_issues',
    description: 'List open issues across all repos or a specific repo. Use this to understand what work is queued or in progress.',
    parameters: {
        type: 'object',
        properties: {
            repo: { type: 'string', description: 'Optional: "owner/name" to scope to one repo. Omit for all repos.' },
            state: { type: 'string', description: '"open", "closed", or "all". Default: "open"' }
        }
    },
    async handler(args, ctx) {
        return api.listAllIssues(ctx.gitea, {
            repo: args.repo ? String(args.repo) : undefined,
            state: args.state ? String(args.state) : undefined,
        });
    }
});

registerTool({
    name: 'read_repo_file',
    description: 'Read a file from any Gitea repository without cloning it. Useful for understanding project structure.',
    parameters: {
        type: 'object',
        properties: {
            repo: { type: 'string', description: 'Repo in "owner/name" format' },
            path: { type: 'string', description: 'File path within the repo (e.g. "src/app/page.tsx")' }
        },
        required: ['repo', 'path']
    },
    async handler(args, ctx) {
        return api.readRepoFile(ctx.gitea, String(args.repo), String(args.path));
    }
});
