import {
  createProjectSchema,
  updateProjectSchema,
  createTaskSchema,
  updateTaskSchema,
  listProjectsSchema,
  listTasksSchema,
  reorderTasksSchema
} from '../../validation/schemas.js';

describe('Validation Schemas', () => {
  describe('createProjectSchema', () => {
    it('should validate valid project data', () => {
      const data = {
        name: 'Test Project',
        description: 'A test project',
        color_hex: '#3498db',
        icon_name: 'rocket'
      };
      
      const result = createProjectSchema.parse(data);
      expect(result.name).toBe('Test Project');
      expect(result.color_hex).toBe('#3498db');
    });
    
    it('should require project name', () => {
      expect(() => createProjectSchema.parse({
        description: 'No name'
      })).toThrow();
    });
    
    it('should enforce name max length', () => {
      expect(() => createProjectSchema.parse({
        name: 'a'.repeat(256)
      })).toThrow();
    });
    
    it('should validate color format', () => {
      expect(() => createProjectSchema.parse({
        name: 'Test',
        color_hex: 'invalid'
      })).toThrow();
    });
    
    it('should use default color', () => {
      const result = createProjectSchema.parse({
        name: 'Test'
      });
      
      expect(result.color_hex).toBe('#3498db');
    });
    
    it('should validate icon name', () => {
      expect(() => createProjectSchema.parse({
        name: 'Test',
        icon_name: 'invalid_icon'
      })).toThrow();
    });
  });
  
  describe('updateProjectSchema', () => {
    it('should allow partial updates', () => {
      const result = updateProjectSchema.parse({
        name: 'Updated'
      });
      
      expect(result.name).toBe('Updated');
      expect(result.status).toBeUndefined();
    });
    
    it('should validate status enum', () => {
      expect(() => updateProjectSchema.parse({
        status: 'invalid'
      })).toThrow();
    });
  });
  
  describe('createTaskSchema', () => {
    it('should validate valid task data', () => {
      const data = {
        title: 'New Task',
        description: 'Task description',
        status: 'backlog',
        due_date: '2026-04-20',
        assignee_id: 1
      };
      
      const result = createTaskSchema.parse(data);
      expect(result.title).toBe('New Task');
      expect(result.status).toBe('backlog');
    });
    
    it('should require task title', () => {
      expect(() => createTaskSchema.parse({
        description: 'No title'
      })).toThrow();
    });
    
    it('should validate date format', () => {
      expect(() => createTaskSchema.parse({
        title: 'Test',
        due_date: 'invalid-date'
      })).toThrow();
    });
    
    it('should use default status', () => {
      const result = createTaskSchema.parse({
        title: 'Test'
      });
      
      expect(result.status).toBe('backlog');
    });
  });
  
  describe('updateTaskSchema', () => {
    it('should allow position updates', () => {
      const result = updateTaskSchema.parse({
        position: 5
      });
      
      expect(result.position).toBe(5);
    });
    
    it('should validate position is non-negative', () => {
      expect(() => updateTaskSchema.parse({
        position: -1
      })).toThrow();
    });
  });
  
  describe('listProjectsSchema', () => {
    it('should parse query parameters', () => {
      const result = listProjectsSchema.parse({
        status: 'active',
        limit: '50',
        offset: '0'
      });
      
      expect(result.status).toBe('active');
      expect(result.limit).toBe(50);
      expect(result.offset).toBe(0);
    });
    
    it('should enforce max limit', () => {
      expect(() => listProjectsSchema.parse({
        limit: '101'
      })).toThrow();
    });
    
    it('should use defaults', () => {
      const result = listProjectsSchema.parse({});
      
      expect(result.status).toBe('active');
      expect(result.limit).toBe(50);
      expect(result.offset).toBe(0);
    });
  });
  
  describe('reorderTasksSchema', () => {
    it('should validate task ID array', () => {
      const result = reorderTasksSchema.parse({
        order: [3, 1, 2]
      });
      
      expect(result.order).toEqual([3, 1, 2]);
    });
    
    it('should require non-empty order', () => {
      expect(() => reorderTasksSchema.parse({
        order: []
      })).toThrow();
    });
  });
});
