/**
 * Tests for Best Practices Template
 */

import { BestPracticesTemplate } from './best-practices-template';
import { TemplateContext } from './base-template';
import { DocumentationIntent } from '../../../types/workflow';

describe('BestPracticesTemplate', () => {
  let template: BestPracticesTemplate;

  beforeEach(() => {
    template = new BestPracticesTemplate();
  });

  it('should handle best_practices intent type', () => {
    const intent: DocumentationIntent = { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
    };

    expect(template.canHandle(intent)).toBe(true);
  });

  it('should not handle other intent types', () => {
    const intent: DocumentationIntent = { type: 'tutorial', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
    };

    expect(template.canHandle(intent)).toBe(false);
  });

  it('should generate general principles section', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test Application',
          version: '1.0.0',
          description: 'Test app description',
        },
        stack: {},
      },
      extendedWorkflow: {},
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('# Best Practices');
    expect(result).toContain('## General Principles');
    expect(result).toContain('### Code Quality');
    expect(result).toContain('### Project Structure');
    expect(result).toContain('### Error Handling');
  });

  it('should generate TypeScript/Express backend practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        technology: {
          backend: {
            framework: 'express',
            language: 'typescript',
            orm: 'prisma',
          },
       
          database: {
            type: 'postgresql',
            version: '15',
          },
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Backend Best Practices');
    expect(result).toContain('### TypeScript/Express');
    expect(result).toContain('**Type Safety:**');
    expect(result).toContain('**Middleware:**');
    expect(result).toContain('**Route Handlers:**');
    expect(result).toContain('### Prisma Best Practices');
  });

  it('should generate Python/Flask backend practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        technology: {
          backend: {
            framework: 'flask',
            language: 'python',
          },
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Backend Best Practices');
    expect(result).toContain('### Python/Flask');
    expect(result).toContain('**Code Style:**');
    expect(result).toContain('**Blueprints:**');
    expect(result).toContain('**Decorators:**');
  });

  it('should generate React frontend practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        technology: {
          frontend: {
            framework: 'react',
            language: 'typescript',
            styling: 'tailwind',
          },
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Frontend Best Practices');
    expect(result).toContain('### React Best Practices');
    expect(result).toContain('**Component Design:**');
    expect(result).toContain('**State Management:**');
    expect(result).toContain('**Hooks:**');
    expect(result).toContain('**Performance:**');
    expect(result).toContain('### Styling with Tailwind');
  });

  it('should generate database practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        technology: {
          database: {
            type: 'postgresql',
            version: '15',
          },
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Database Best Practices');
    expect(result).toContain('### Schema Design');
    expect(result).toContain('### Queries');
    expect(result).toContain('### Migrations');
  });

  it('should generate API practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        api: {
          base_path: '/api',
          endpoints: [],
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## API Best Practices');
    expect(result).toContain('### RESTful Design');
    expect(result).toContain('### Request/Response');
    expect(result).toContain('### Status Codes');
  });

  it('should generate security practices with auth', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        auth: {
          enabled: true,
          provider: 'clerk',
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Security Best Practices');
    expect(result).toContain('### Authentication');
    expect(result).toContain('### Authorization');
    expect(result).toContain('### API Security');
    expect(result).toContain('### Clerk-Specific');
    expect(result).toContain('### Environment Variables');
  });

  it('should generate testing practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {
        technology: {
          testing: {
            unit: 'vitest',
            e2e: 'playwright',
          },
        },
      },
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Testing Best Practices');
    expect(result).toContain('### Unit Tests');
    expect(result).toContain('### Integration Tests');
    expect(result).toContain('### E2E Tests');
    expect(result).toContain('### Test Organization');
  });

  it('should generate git workflow practices', () => {
    const context: TemplateContext = {
      intent: { type: 'best_practices', audience: 'developer', overwrite: 'always', includeExamples: false, includeCommonIssues: false,
      },
      workflow: {
        workflow: {
          id: 'test-app',
          name: 'Test App',
          version: '1.0.0',
          description: 'Test',
        },
        stack: {},
      },
      extendedWorkflow: {},
      versions: {},
      outputDirectory: '/test',
    };

    const result = template.generate(context);

    expect(result).toContain('## Git Workflow');
    expect(result).toContain('### Branches');
    expect(result).toContain('### Commits');
    expect(result).toContain('### Pull Requests');
  });
});
