// =============================================================================
// Coolify tool registrations (in-process path used by agent-runner).
//
// All logic lives in ./coolify-api.ts so the MCP server (src/mcp/coolify-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.coolify → CoolifyConfig. No business logic here.
// =============================================================================

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

registerTool({
    name: 'coolify_list_projects',
    description: 'List all projects in the Coolify instance. Returns project names and UUIDs.',
    parameters: { type: 'object', properties: {} },
    async handler(_args, ctx) {
        return api.listProjects(ctx.coolify);
    }
});

registerTool({
    name: 'coolify_list_applications',
    description: 'List applications in a Coolify project.',
    parameters: {
        type: 'object',
        properties: {
            project_uuid: { type: 'string', description: 'Project UUID from coolify_list_projects' }
        },
        required: ['project_uuid']
    },
    async handler(args, ctx) {
        return api.listApplications(ctx.coolify, String(args.project_uuid));
    }
});

registerTool({
    name: 'coolify_deploy',
    description: 'Trigger a deployment for a Coolify application.',
    parameters: {
        type: 'object',
        properties: {
            application_uuid: { type: 'string', description: 'Application UUID to deploy' }
        },
        required: ['application_uuid']
    },
    async handler(args, ctx) {
        return api.deploy(ctx.coolify, String(args.application_uuid));
    }
});

registerTool({
    name: 'coolify_get_logs',
    description: 'Get recent deployment logs for a Coolify application.',
    parameters: {
        type: 'object',
        properties: {
            application_uuid: { type: 'string', description: 'Application UUID' }
        },
        required: ['application_uuid']
    },
    async handler(args, ctx) {
        return api.getLogs(ctx.coolify, String(args.application_uuid));
    }
});

registerTool({
    name: 'list_all_apps',
    description: 'List all Coolify applications across all projects with their status (running/stopped/error) and domain.',
    parameters: { type: 'object', properties: {} },
    async handler(_args, ctx) {
        return api.listAllApps(ctx.coolify);
    }
});

registerTool({
    name: 'get_app_status',
    description: 'Get the current deployment status and recent logs for a specific Coolify application by name or UUID.',
    parameters: {
        type: 'object',
        properties: {
            app_name: { type: 'string', description: 'Application name (e.g. "vibn-frontend") or UUID' }
        },
        required: ['app_name']
    },
    async handler(args, ctx) {
        return api.getAppStatus(ctx.coolify, String(args.app_name));
    }
});

registerTool({
    name: 'deploy_app',
    description: 'Trigger a Coolify deployment for an app by name. Use after an agent commits code.',
    parameters: {
        type: 'object',
        properties: {
            app_name: { type: 'string', description: 'Application name (e.g. "vibn-frontend")' }
        },
        required: ['app_name']
    },
    async handler(args, ctx) {
        return api.deployApp(ctx.coolify, String(args.app_name));
    }
});
