import { z } from 'zod';

// Enum schemas
export const TaskPrioritySchema = z.enum(['lowest', 'low', 'medium', 'high', 'highest']);
export const TaskTypeSchema = z.enum(['feature', 'bug', 'enhancement', 'research', 'maintenance']);
export const TaskComplexitySchema = z.enum(['trivial', 'minor', 'major', 'critical']);

// Base task schema
export const TaskBaseSchema = z.object({
  title: z.string().min(1).max(255),
  description: z.string().optional(),
  status: z.string().uuid().optional(),
  priority: TaskPrioritySchema.default('medium'),
  task_type: TaskTypeSchema.optional(),
  complexity: TaskComplexitySchema.optional(),
  story_points: z.number().int().min(1).max(21).optional(),
  project: z.string().uuid(),
  parent_task: z.string().uuid().optional(),
  epic: z.string().uuid().optional(),
  assigned_to: z.string().uuid().optional(),
  ai_agent_assigned: z.string().max(255).optional(),
  reviewer: z.string().uuid().optional(),
  estimated_hours: z.number().positive().optional(),
  progress_percentage: z.number().int().min(0).max(100).default(0),
  start_date: z.string().optional(),
  due_date: z.string().optional(),
  task_master_id: z.string().max(255).optional(),
  github_issue_url: z.string().url().optional(),
  bmad_story_id: z.string().max(255).optional(),
  external_refs: z.record(z.string(), z.any()).optional(),
  ai_generated: z.boolean().default(false),
  ai_context: z.record(z.string(), z.any()).optional(),
  auto_status_updates: z.boolean().default(false),
  acceptance_criteria: z.string().optional(),
  definition_of_done: z.string().optional(),
  notes: z.string().optional(),
});

// Create task schema
export const CreateTaskSchema = TaskBaseSchema;

// Update task schema (all fields optional)
export const UpdateTaskSchema = TaskBaseSchema.partial();

// Bulk operation schemas
export const BulkCreateTasksSchema = z.object({
  tasks: z.array(CreateTaskSchema).min(1).max(100),
});

export const BulkUpdateTasksSchema = z.object({
  ids: z.array(z.string().uuid()).min(1).max(100),
  updates: UpdateTaskSchema,
});

export const BulkDeleteTasksSchema = z.object({
  ids: z.array(z.string().uuid()).min(1).max(100),
});

// Task duplication schema
export const DuplicateTaskSchema = z.object({
  taskId: z.string().uuid(),
  includeSubtasks: z.boolean().default(false),
  includeAttachments: z.boolean().default(false),
  includeComments: z.boolean().default(false),
  overrides: UpdateTaskSchema.optional(),
});

// Task template schemas
export const CreateTemplateFromTaskSchema = z.object({
  taskId: z.string().uuid(),
  templateName: z.string().min(1).max(255),
  templateDescription: z.string().optional(),
  excludeFields: z.array(z.string()).optional(),
});

export const ApplyTemplateSchema = z.object({
  templateId: z.string().uuid(),
  projectId: z.string().uuid(),
  overrides: UpdateTaskSchema.optional(),
});

// Query parameter schemas
export const TaskQuerySchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().min(1).max(100).default(20),
  sort: z.string().optional(),
  filter: z.record(z.string(), z.any()).optional(),
  search: z.string().optional(),
  fields: z.array(z.string()).optional(),
});

// Response schemas
export const TaskResponseSchema = TaskBaseSchema.extend({
  id: z.string().uuid(),
  created_by: z.string().uuid().optional(),
  created_at: z.string(),
  updated_at: z.string(),
  actual_hours: z.number().optional(),
  completed_at: z.string().optional(),
});

export const PaginatedTasksResponseSchema = z.object({
  data: z.array(TaskResponseSchema),
  meta: z.object({
    total: z.number(),
    page: z.number(),
    limit: z.number(),
    totalPages: z.number(),
  }),
});

// Type exports
export type TaskBase = z.infer<typeof TaskBaseSchema>;
export type CreateTask = z.infer<typeof CreateTaskSchema>;
export type UpdateTask = z.infer<typeof UpdateTaskSchema>;
export type TaskResponse = z.infer<typeof TaskResponseSchema>;
export type TaskQuery = z.infer<typeof TaskQuerySchema>;
export type BulkCreateTasks = z.infer<typeof BulkCreateTasksSchema>;
export type BulkUpdateTasks = z.infer<typeof BulkUpdateTasksSchema>;
export type BulkDeleteTasks = z.infer<typeof BulkDeleteTasksSchema>;
export type DuplicateTask = z.infer<typeof DuplicateTaskSchema>;
export type CreateTemplateFromTask = z.infer<typeof CreateTemplateFromTaskSchema>;
export type ApplyTemplate = z.infer<typeof ApplyTemplateSchema>;