import { describe, it, expect } from 'vitest';
import * as agileManagement from '../../modules/agile-management/index.js';
import * as dataManagement from '../../modules/data-management/index.js';
import * as testingFramework from '../../modules/testing-framework/index.js';
import * as kanban from '../../modules/kanban/index.js';
import * as documentation from '../../modules/documentation/index.js';
import * as businessGuidance from '../../modules/business-guidance/index.js';
import * as productRoadmap from '../../modules/product-roadmap/index.js';
import * as codeAnalysis from '../../modules/code-analysis/index.js';
import * as development from '../../modules/development/index.js';
import * as memoryManagement from '../../modules/memory-management/index.js';
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { ConfigManager } from '../../config/config-manager.js';

describe('Tool Schema Validation', () => {
  it('should have required field for all tools with properties', async () => {
    const server = new Server(
      { name: 'test', version: '1.0.0' }, 
      { capabilities: { tools: {}, resources: {} } }
    );
    const configManager = new ConfigManager();
    
    // Collect all modules
    const modules = [
      await agileManagement.setupAgileManagementTools(server, configManager),
      await dataManagement.setupDataManagementTools(server, configManager),
      await testingFramework.setupTestingFrameworkTools(server, configManager),
      await kanban.setupKanbanTools(server, configManager),
      await documentation.setupDocumentationTools(server, configManager),
      await businessGuidance.setupBusinessGuidanceTools(server, configManager),
      await productRoadmap.setupProductRoadmap(server, configManager),
      await codeAnalysis.setupCodeAnalysisTools(server, configManager),
      await development.setupDevelopmentTools(server, configManager),
      await memoryManagement.setupMemoryManagementTools(server, configManager),
    ];
    
    let totalTools = 0;
    let toolsWithProperties = 0;
    let toolsWithoutRequired = [];
    
    for (const module of modules) {
      if (module.tools) {
        for (const tool of module.tools) {
          totalTools++;
          
          if (tool.inputSchema && tool.inputSchema.properties) {
            toolsWithProperties++;
            
            if (!tool.inputSchema.hasOwnProperty('required')) {
              toolsWithoutRequired.push(tool.name);
            } else {
              // Verify required is an array
              expect(Array.isArray(tool.inputSchema.required)).toBe(true);
            }
          }
        }
      }
    }
    
    // Report findings
    console.log(`Total tools: ${totalTools}`);
    console.log(`Tools with properties: ${toolsWithProperties}`);
    console.log(`Tools missing required field: ${toolsWithoutRequired.length}`);
    
    if (toolsWithoutRequired.length > 0) {
      console.error('Tools missing required field:', toolsWithoutRequired);
    }
    
    // Assert all tools with properties have required field
    expect(toolsWithoutRequired).toHaveLength(0);
  });
  
  it('should validate against JSON Schema specification', async () => {
    const server = new Server(
      { name: 'test', version: '1.0.0' }, 
      { capabilities: { tools: {}, resources: {} } }
    );
    const configManager = new ConfigManager();
    
    const module = await agileManagement.setupAgileManagementTools(server, configManager);
    
    for (const tool of module.tools || []) {
      if (tool.inputSchema) {
        // Basic JSON Schema validation
        expect(tool.inputSchema.type).toBe('object');
        
        if (tool.inputSchema.properties) {
          expect(tool.inputSchema).toHaveProperty('required');
          expect(Array.isArray(tool.inputSchema.required)).toBe(true);
          
          // Check that all required fields exist in properties
          for (const requiredField of tool.inputSchema.required) {
            expect(tool.inputSchema.properties).toHaveProperty(requiredField);
          }
        }
      }
    }
  });
});