import { z } from 'zod';
import { ProductRequirementsStore } from './store.js';
const requirementTypeSchema = z.enum([
    'functional',
    'non-functional',
    'business',
    'technical',
    'user-interface',
    'security',
    'performance'
]);
const requirementStatusSchema = z.enum([
    'draft',
    'approved',
    'in_development',
    'implemented',
    'deprecated'
]);
const requirementPrioritySchema = z.enum(['low', 'medium', 'high', 'critical']);
const implementationStatusSchema = z.enum(['not_started', 'in_progress', 'completed', 'blocked']);
const repositorySchema = z.object({
    name: z.string(),
    implementation_status: implementationStatusSchema,
    branch: z.string().optional(),
    pr_url: z.string().optional(),
    notes: z.string().optional()
});
export async function setupProductRequirements(server) {
    const store = new ProductRequirementsStore();
    const tools = [
        {
            name: 'create_requirement',
            description: 'Create a new product requirement',
            inputSchema: {
                type: 'object',
                properties: {
                    name: {
                        type: 'string',
                        description: 'Name of the requirement'
                    },
                    type: {
                        type: 'string',
                        enum: requirementTypeSchema.options,
                        description: 'Type of requirement'
                    },
                    description: {
                        type: 'string',
                        description: 'Detailed description of the requirement'
                    },
                    acceptance_criteria: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'List of acceptance criteria'
                    },
                    priority: {
                        type: 'string',
                        enum: requirementPrioritySchema.options,
                        description: 'Priority level'
                    },
                    status: {
                        type: 'string',
                        enum: requirementStatusSchema.options,
                        description: 'Current status'
                    },
                    repositories: {
                        type: 'array',
                        items: {
                            type: 'object',
                            properties: {
                                name: { type: 'string' },
                                implementation_status: {
                                    type: 'string',
                                    enum: implementationStatusSchema.options
                                },
                                branch: { type: 'string' },
                                pr_url: { type: 'string' },
                                notes: { type: 'string' }
                            },
                            required: ['name', 'implementation_status']
                        },
                        description: 'Repositories where this requirement is implemented'
                    },
                    related_stories: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'IDs of related user stories'
                    },
                    tags: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Tags for categorization'
                    },
                    business_value: {
                        type: 'string',
                        description: 'Business value statement'
                    },
                    technical_notes: {
                        type: 'string',
                        description: 'Technical implementation notes'
                    },
                    test_criteria: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Testing criteria'
                    },
                    compliance_requirements: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Compliance and regulatory requirements'
                    },
                    estimated_effort: {
                        type: 'string',
                        description: 'Estimated effort (e.g., "2 weeks", "5 story points")'
                    },
                    target_release: {
                        type: 'string',
                        description: 'Target release version'
                    },
                    parent_requirement_id: {
                        type: 'string',
                        description: 'ID of parent requirement for hierarchical organization'
                    }
                },
                required: ['name', 'type', 'description']
            }
        },
        {
            name: 'list_requirements',
            description: 'List all product requirements with optional filtering',
            inputSchema: {
                type: 'object',
                properties: {
                    filter_by_type: {
                        type: 'string',
                        enum: requirementTypeSchema.options,
                        description: 'Filter by requirement type'
                    },
                    filter_by_status: {
                        type: 'string',
                        enum: requirementStatusSchema.options,
                        description: 'Filter by status'
                    },
                    filter_by_priority: {
                        type: 'string',
                        enum: requirementPrioritySchema.options,
                        description: 'Filter by priority'
                    },
                    filter_by_repository: {
                        type: 'string',
                        description: 'Filter by repository name'
                    },
                    filter_by_tag: {
                        type: 'string',
                        description: 'Filter by tag'
                    },
                    filter_by_parent: {
                        type: 'string',
                        description: 'Filter by parent requirement ID'
                    },
                    has_stories: {
                        type: 'boolean',
                        description: 'Filter requirements with/without related stories'
                    },
                    implementation_status: {
                        type: 'string',
                        enum: implementationStatusSchema.options,
                        description: 'Filter by implementation status in any repository'
                    }
                },
                required: []
            }
        },
        {
            name: 'get_requirement',
            description: 'Get a specific product requirement by ID',
            inputSchema: {
                type: 'object',
                properties: {
                    id: {
                        type: 'string',
                        description: 'Requirement ID'
                    }
                },
                required: ['id']
            }
        },
        {
            name: 'update_requirement',
            description: 'Update an existing product requirement',
            inputSchema: {
                type: 'object',
                properties: {
                    id: {
                        type: 'string',
                        description: 'Requirement ID to update'
                    },
                    name: {
                        type: 'string',
                        description: 'Updated name'
                    },
                    type: {
                        type: 'string',
                        enum: requirementTypeSchema.options,
                        description: 'Updated type'
                    },
                    description: {
                        type: 'string',
                        description: 'Updated description'
                    },
                    acceptance_criteria: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Updated acceptance criteria'
                    },
                    priority: {
                        type: 'string',
                        enum: requirementPrioritySchema.options,
                        description: 'Updated priority'
                    },
                    status: {
                        type: 'string',
                        enum: requirementStatusSchema.options,
                        description: 'Updated status'
                    },
                    repositories: {
                        type: 'array',
                        items: {
                            type: 'object',
                            properties: {
                                name: { type: 'string' },
                                implementation_status: {
                                    type: 'string',
                                    enum: implementationStatusSchema.options
                                },
                                branch: { type: 'string' },
                                pr_url: { type: 'string' },
                                notes: { type: 'string' }
                            },
                            required: ['name', 'implementation_status']
                        },
                        description: 'Updated repository information'
                    },
                    tags: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Updated tags'
                    },
                    business_value: {
                        type: 'string',
                        description: 'Updated business value'
                    },
                    technical_notes: {
                        type: 'string',
                        description: 'Updated technical notes'
                    },
                    test_criteria: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Updated test criteria'
                    },
                    compliance_requirements: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Updated compliance requirements'
                    },
                    estimated_effort: {
                        type: 'string',
                        description: 'Updated estimated effort'
                    },
                    actual_effort: {
                        type: 'string',
                        description: 'Actual effort spent'
                    },
                    target_release: {
                        type: 'string',
                        description: 'Updated target release'
                    },
                    documentation_links: {
                        type: 'array',
                        items: { type: 'string' },
                        description: 'Links to documentation'
                    }
                },
                required: ['id']
            }
        },
        {
            name: 'delete_requirement',
            description: 'Delete a product requirement',
            inputSchema: {
                type: 'object',
                properties: {
                    id: {
                        type: 'string',
                        description: 'Requirement ID to delete'
                    }
                },
                required: ['id']
            }
        },
        {
            name: 'search_requirements',
            description: 'Search product requirements by text query',
            inputSchema: {
                type: 'object',
                properties: {
                    query: {
                        type: 'string',
                        description: 'Search query'
                    },
                    search_in: {
                        type: 'array',
                        items: {
                            type: 'string',
                            enum: ['name', 'description', 'acceptance_criteria', 'tags']
                        },
                        description: 'Fields to search in (default: name, description)'
                    },
                    limit: {
                        type: 'number',
                        description: 'Maximum results to return'
                    },
                    offset: {
                        type: 'number',
                        description: 'Offset for pagination'
                    }
                },
                required: ['query']
            }
        },
        {
            name: 'link_requirement_to_story',
            description: 'Link a product requirement to a user story',
            inputSchema: {
                type: 'object',
                properties: {
                    requirement_id: {
                        type: 'string',
                        description: 'Product requirement ID'
                    },
                    story_id: {
                        type: 'string',
                        description: 'User story ID'
                    },
                    link_type: {
                        type: 'string',
                        enum: ['implements', 'relates_to', 'depends_on'],
                        description: 'Type of relationship'
                    },
                    notes: {
                        type: 'string',
                        description: 'Additional notes about the link'
                    }
                },
                required: ['requirement_id', 'story_id']
            }
        },
        {
            name: 'update_implementation_status',
            description: 'Update implementation status for a requirement in a specific repository',
            inputSchema: {
                type: 'object',
                properties: {
                    requirement_id: {
                        type: 'string',
                        description: 'Product requirement ID'
                    },
                    repository_name: {
                        type: 'string',
                        description: 'Repository name'
                    },
                    status: {
                        type: 'string',
                        enum: implementationStatusSchema.options,
                        description: 'New implementation status'
                    },
                    branch: {
                        type: 'string',
                        description: 'Branch name where implementation is happening'
                    },
                    pr_url: {
                        type: 'string',
                        description: 'Pull request URL'
                    },
                    notes: {
                        type: 'string',
                        description: 'Implementation notes'
                    }
                },
                required: ['requirement_id', 'repository_name', 'status']
            }
        },
        {
            name: 'generate_requirements_report',
            description: 'Generate a comprehensive requirements report',
            inputSchema: {
                type: 'object',
                properties: {
                    format: {
                        type: 'string',
                        enum: ['json', 'markdown', 'html'],
                        description: 'Report format (default: json)'
                    },
                    include_implementation_status: {
                        type: 'boolean',
                        description: 'Include detailed implementation status'
                    },
                    group_by: {
                        type: 'string',
                        enum: ['type', 'status', 'priority'],
                        description: 'Group requirements by this field'
                    },
                    filter_type: {
                        type: 'string',
                        enum: requirementTypeSchema.options,
                        description: 'Filter by requirement type'
                    },
                    filter_status: {
                        type: 'string',
                        enum: requirementStatusSchema.options,
                        description: 'Filter by status'
                    },
                    filter_priority: {
                        type: 'string',
                        enum: requirementPrioritySchema.options,
                        description: 'Filter by priority'
                    }
                },
                required: []
            }
        }
    ];
    // Return tools and resources without setting up handlers
    // The handlers will be set up in the main server
    const resources = [
        {
            uri: 'atlas://requirements',
            name: 'Product Requirements',
            mimeType: 'application/json',
            description: 'All product requirements in the system'
        }
    ];
    return { tools, resources };
}
// Export handler functions for the server to use
export async function handleProductRequirementsTool(name, args) {
    const store = new ProductRequirementsStore();
    switch (name) {
        case 'create_requirement':
            const requirement = await store.createRequirement(args);
            return {
                content: [{
                        type: 'text',
                        text: `Created requirement "${requirement.name}" with ID ${requirement.id}`
                    }],
                _meta: { requirement }
            };
        case 'list_requirements':
            const filter = {
                type: args.filter_by_type,
                status: args.filter_by_status,
                priority: args.filter_by_priority,
                repository: args.filter_by_repository,
                tag: args.filter_by_tag,
                parent_id: args.filter_by_parent,
                has_stories: args.has_stories,
                implementation_status: args.implementation_status
            };
            const requirements = await store.listRequirements(filter);
            return {
                content: [{
                        type: 'text',
                        text: `Found ${requirements.length} requirement(s)`
                    }],
                _meta: { requirements }
            };
        case 'get_requirement':
            const reqToGet = await store.getRequirement(args.id);
            if (!reqToGet) {
                return {
                    content: [{
                            type: 'text',
                            text: `Requirement ${args.id} not found`
                        }],
                    isError: true
                };
            }
            return {
                content: [{
                        type: 'text',
                        text: `Retrieved requirement "${reqToGet.name}"`
                    }],
                _meta: { requirement: reqToGet }
            };
        case 'update_requirement':
            const updated = await store.updateRequirement(args);
            if (!updated) {
                return {
                    content: [{
                            type: 'text',
                            text: `Requirement ${args.id} not found`
                        }],
                    isError: true
                };
            }
            return {
                content: [{
                        type: 'text',
                        text: `Updated requirement "${updated.name}"`
                    }],
                _meta: { requirement: updated }
            };
        case 'delete_requirement':
            const deleted = await store.deleteRequirement(args.id);
            if (!deleted) {
                return {
                    content: [{
                            type: 'text',
                            text: `Requirement ${args.id} not found`
                        }],
                    isError: true
                };
            }
            return {
                content: [{
                        type: 'text',
                        text: `Deleted requirement ${args.id}`
                    }]
            };
        case 'search_requirements':
            const searchResults = await store.searchRequirements({
                query: args.query,
                search_in: args.search_in,
                limit: args.limit,
                offset: args.offset
            });
            return {
                content: [{
                        type: 'text',
                        text: `Found ${searchResults.length} requirement(s) matching "${args.query}"`
                    }],
                _meta: { requirements: searchResults }
            };
        case 'link_requirement_to_story':
            try {
                const link = await store.linkRequirementToStory(args.requirement_id, args.story_id, args.link_type, args.notes);
                return {
                    content: [{
                            type: 'text',
                            text: `Linked requirement ${args.requirement_id} to story ${args.story_id}`
                        }],
                    _meta: { link }
                };
            }
            catch (error) {
                return {
                    content: [{
                            type: 'text',
                            text: error.message
                        }],
                    isError: true
                };
            }
        case 'update_implementation_status':
            const statusUpdate = await store.updateImplementationStatus(args);
            if (!statusUpdate) {
                return {
                    content: [{
                            type: 'text',
                            text: `Requirement ${args.requirement_id} not found`
                        }],
                    isError: true
                };
            }
            return {
                content: [{
                        type: 'text',
                        text: `Updated implementation status for repository "${args.repository_name}" to "${args.status}"`
                    }],
                _meta: { requirement: statusUpdate }
            };
        case 'generate_requirements_report':
            const reportFilter = (args.filter_type || args.filter_status || args.filter_priority) ? {
                type: args.filter_type,
                status: args.filter_status,
                priority: args.filter_priority
            } : undefined;
            const report = await store.generateRequirementsReport({
                format: args.format,
                include_implementation_status: args.include_implementation_status,
                filter: reportFilter,
                group_by: args.group_by
            });
            return {
                content: [{
                        type: 'text',
                        text: `Generated requirements report with ${report.requirements.length} requirement(s)`
                    }],
                _meta: { report }
            };
        default:
            return {
                content: [{
                        type: 'text',
                        text: `Unknown tool: ${name}`
                    }],
                isError: true
            };
    }
}
export async function handleProductRequirementsResource(uri) {
    if (uri === 'atlas://requirements') {
        const store = new ProductRequirementsStore();
        const requirements = await store.listRequirements();
        return {
            contents: [{
                    uri: 'atlas://requirements',
                    mimeType: 'application/json',
                    text: JSON.stringify(requirements, null, 2)
                }]
        };
    }
    throw new Error(`Unknown resource: ${uri}`);
}
//# sourceMappingURL=index.js.map