import { z } from 'zod';
import { t } from '../../i18n/index.js';
import { safeTracked, safeLicensed } from '../safe-handler.js';
import { SupportedLocaleEnum, SpecStatusEnum, SpecTypeEnum, SpecScopeEnum, SpecTargetEnum, ExperienceLevelEnum, ListSpecsOutputSchema, EstimateOutputSchema, ValidateOutputSchema, } from '../schemas/index.js';
import { handleSetLocale } from '../set-locale.js';
import { handleInitProject } from '../init-project.js';
import { handleInitConstitution } from '../init-constitution.js';
import { handleClarifyRequirements } from '../clarify-requirements.js';
import { handleCreateSpec } from '../create-spec.js';
import { handleListSpecs } from '../list-specs.js';
import { handleUpdateStatus } from '../update-status.js';
import { handleEstimate } from '../estimate.js';
import { handleReverseEngineer } from '../reverse-engineer.js';
import { handleValidate } from '../validate.js';
/**
 * Registers core spec lifecycle tools (tools 1–10) on the MCP server.
 */
export function registerCoreSpecTools(server) {
    // 1. set_locale
    server.registerTool('set_locale', {
        description: t('tools.set_locale.description'),
        inputSchema: {
            locale: SupportedLocaleEnum.describe('Target locale (en, es, pt)'),
            projectId: z
                .string()
                .max(500)
                .optional()
                .describe('Optional project ID to set locale per-project'),
        },
    }, safeTracked('set_locale', async (args) => handleSetLocale(args)));
    // 2. init_project
    server.registerTool('init_project', {
        description: t('tools.init_project.description'),
        inputSchema: {
            projectPath: z.string().max(4096).describe('Absolute path to the project root'),
            locale: SupportedLocaleEnum.optional().describe('Preferred locale for this project'),
            hourlyRate: z
                .number()
                .optional()
                .describe('Developer hourly rate in USD for cost estimation'),
            experienceLevel: ExperienceLevelEnum.optional().describe('Developer experience level: beginner, intermediate, or expert'),
            userProfile: z
                .enum(['developer', 'product-owner', 'designer', 'non-technical'])
                .optional()
                .describe('User profile type — determines how detailed and technical Planu interactions will be. Ask the user before running init_project.'),
        },
    }, safeTracked('init_project', async (args) => handleInitProject(args)));
    // 3. init_constitution
    server.registerTool('init_constitution', {
        description: t('tools.init_constitution.description'),
        inputSchema: {
            projectId: z.string().max(500).describe('Project ID'),
            autoDetect: z
                .boolean()
                .optional()
                .describe('Auto-detect principles from codebase (default: true)'),
            principles: z
                .array(z.object({
                id: z.string().max(500),
                category: z.enum([
                    'architecture',
                    'security',
                    'quality',
                    'conventions',
                    'tech-stack',
                    'workflow',
                ]),
                principle: z.string().max(10_000),
                rationale: z.string().max(10_000),
                enforceLevel: z.enum(['strict', 'recommended']),
                autoEnforceable: z.boolean(),
                source: z.enum(['auto-detected', 'user-defined', 'claude-md', 'learned']),
                immutable: z.boolean(),
            }))
                .max(1000)
                .optional()
                .describe('Manual principles to include'),
        },
    }, safeTracked('init_constitution', async (args) => handleInitConstitution(args)));
    // 4. clarify_requirements
    server.registerTool('clarify_requirements', {
        description: t('tools.clarify_requirements.description'),
        inputSchema: {
            projectId: z.string().max(500).describe('Project ID'),
            topic: z.string().max(500).describe('The topic or feature to clarify'),
            context: z
                .string()
                .max(10_000)
                .optional()
                .describe('Additional context about the requirement'),
            maxQuestions: z
                .number()
                .optional()
                .describe('Maximum number of questions to generate (default: 5)'),
        },
    }, safeTracked('clarify_requirements', async (args) => handleClarifyRequirements(args)));
    // 5. create_spec
    server.registerTool('create_spec', {
        description: t('tools.create_spec.description'),
        inputSchema: {
            title: z.string().max(500).describe('Title of the spec / user story'),
            description: z
                .string()
                .max(10_000)
                .describe('Detailed description of the feature or change'),
            projectPath: z.string().max(4096).describe('Absolute path to the project root'),
            type: SpecTypeEnum.optional().describe('Spec type (default: feature)'),
            scope: SpecScopeEnum.optional().describe('Spec scope (default: feature)'),
            target: SpecTargetEnum.optional().describe('Target area: frontend, backend, shared, fullstack, infrastructure, database'),
            tags: z.array(z.string().max(500)).max(100).optional().describe('Tags for categorization'),
            feature: z
                .string()
                .max(500)
                .optional()
                .describe('Feature group tag (e.g. "Authentication", "Billing"). Added to tags for categorization. Flat structure — no subdirectories created.'),
        },
    }, safeTracked('create_spec', async (args) => handleCreateSpec(args)));
    // 6. list_specs
    server.registerTool('list_specs', {
        description: t('tools.list_specs.description'),
        inputSchema: {
            projectId: z.string().max(500).describe('Project ID'),
            status: SpecStatusEnum.optional().describe('Filter by spec status'),
            type: SpecTypeEnum.optional().describe('Filter by spec type'),
        },
        outputSchema: ListSpecsOutputSchema,
    }, safeTracked('list_specs', async (args) => handleListSpecs(args)));
    // 7. update_status
    server.registerTool('update_status', {
        description: t('tools.update_status.description'),
        inputSchema: {
            specId: z.string().max(500).describe('Spec ID to update'),
            projectId: z.string().max(500).describe('Project ID'),
            status: SpecStatusEnum.describe('New status'),
            actuals: z
                .object({
                devHours: z.number(),
                reviewHours: z.number(),
                tokensOpus: z.number(),
                tokensSonnet: z.number(),
                apiCostUsd: z.number(),
                humanCostUsd: z.number(),
                totalCostUsd: z.number(),
                completedAt: z.string().max(500),
                notes: z.string().max(10_000),
            })
                .optional()
                .describe('Actual metrics (required when status = done)'),
            reviewNotes: z
                .string()
                .max(10_000)
                .optional()
                .describe('Feedback or observations when sending a spec back (e.g., review→draft). Stored in the spec for the implementer to address.'),
            force: z
                .boolean()
                .optional()
                .describe('Force transition to done bypassing DoD gates. Use when validate already confirmed the spec is ready but auto-check gates cannot be satisfied (e.g., no test files detected, manual review pending).'),
        },
    }, safeTracked('update_status', async (args) => handleUpdateStatus(args)));
    // 8. estimate
    server.registerTool('estimate', {
        description: t('tools.estimate.description'),
        inputSchema: {
            specId: z.string().max(500).describe('Spec ID to estimate'),
            projectId: z.string().max(500).describe('Project ID'),
        },
        outputSchema: EstimateOutputSchema,
    }, safeTracked('estimate', async (args) => handleEstimate(args)));
    // 9. reverse_engineer
    server.registerTool('reverse_engineer', {
        description: t('tools.reverse_engineer.description'),
        inputSchema: {
            path: z.string().max(4096).describe('Path to the file or directory to analyze'),
            projectId: z.string().max(500).describe('Project ID'),
            depth: z
                .enum(['shallow', 'deep', 'deep-v2'])
                .optional()
                .describe('Analysis depth: shallow (fast scan), deep (full file scan), deep-v2 (full scan + test coverage, dependency graph, API detection, complexity metrics, config analysis). Default: shallow'),
        },
    }, safeLicensed('reverse_engineer', async (args) => handleReverseEngineer(args)));
    // 10. validate
    server.registerTool('validate', {
        description: t('tools.validate.description'),
        inputSchema: {
            specId: z.string().max(500).describe('Spec ID to validate'),
            projectId: z.string().max(500).describe('Project ID'),
        },
        outputSchema: ValidateOutputSchema,
    }, safeTracked('validate', async (args) => handleValidate(args)));
}
//# sourceMappingURL=core-spec-tools.js.map