import { z } from 'zod';

// ============================================================================
// PROJECT SCHEMAS
// ============================================================================

export const createProjectSchema = z.object({
  name: z.string()
    .min(1, 'Project name is required')
    .max(255, 'Project name must be 255 characters or less')
    .trim(),
  description: z.string()
    .max(5000, 'Description must be 5000 characters or less')
    .optional()
    .transform(v => v === '' ? undefined : v),
  color_hex: z.string()
    .regex(/^#[0-9a-fA-F]{6}$/, 'Invalid color format. Must be #RRGGBB')
    .optional()
    .default('#3498db'),
  icon_name: z.enum(['rocket', 'bug', 'feature', 'docs', 'deploy', 'design'])
    .optional()
});

export const updateProjectSchema = z.object({
  name: z.string()
    .min(1, 'Project name is required')
    .max(255, 'Project name must be 255 characters or less')
    .trim()
    .optional(),
  description: z.string()
    .max(5000, 'Description must be 5000 characters or less')
    .optional()
    .transform(v => v === '' ? undefined : v),
  status: z.enum(['active', 'archived', 'paused']).optional(),
  color_hex: z.string()
    .regex(/^#[0-9a-fA-F]{6}$/, 'Invalid color format. Must be #RRGGBB')
    .optional(),
  icon_name: z.enum(['rocket', 'bug', 'feature', 'docs', 'deploy', 'design'])
    .optional()
}).strict();

export const listProjectsSchema = z.object({
  status: z.enum(['active', 'archived', 'paused']).optional().default('active'),
  limit: z.coerce.number()
    .min(1, 'Limit must be at least 1')
    .max(100, 'Limit must be at most 100')
    .optional()
    .default(50),
  offset: z.coerce.number()
    .min(0, 'Offset must be non-negative')
    .optional()
    .default(0)
}).strict();

// ============================================================================
// TASK SCHEMAS
// ============================================================================

export const createTaskSchema = z.object({
  title: z.string()
    .min(1, 'Task title is required')
    .max(500, 'Task title must be 500 characters or less')
    .trim(),
  description: z.string()
    .max(10000, 'Description must be 10000 characters or less')
    .optional()
    .transform(v => v === '' ? undefined : v),
  status: z.enum(['backlog', 'in_progress', 'done', 'blocked'])
    .optional()
    .default('backlog'),
  due_date: z.string()
    .regex(/^\d{4}-\d{2}-\d{2}$/, 'Invalid date format. Must be YYYY-MM-DD')
    .optional(),
  assignee_id: z.coerce.number().optional()
});

export const updateTaskSchema = z.object({
  title: z.string()
    .min(1, 'Task title is required')
    .max(500, 'Task title must be 500 characters or less')
    .trim()
    .optional(),
  description: z.string()
    .max(10000, 'Description must be 10000 characters or less')
    .optional()
    .transform(v => v === '' ? undefined : v),
  status: z.enum(['backlog', 'in_progress', 'done', 'blocked']).optional(),
  due_date: z.string()
    .regex(/^\d{4}-\d{2}-\d{2}$/, 'Invalid date format. Must be YYYY-MM-DD')
    .optional()
    .nullable(),
  assignee_id: z.coerce.number().optional().nullable(),
  position: z.coerce.number().min(0, 'Position must be non-negative').optional()
}).strict();

export const listTasksSchema = z.object({
  status: z.enum(['backlog', 'in_progress', 'done', 'blocked', 'all'])
    .optional()
    .default('all'),
  sort: z.enum(['position', 'due_date', 'created_at', '-updated_at'])
    .optional()
    .default('position'),
  limit: z.coerce.number()
    .min(1, 'Limit must be at least 1')
    .max(500, 'Limit must be at most 500')
    .optional()
    .default(200),
  offset: z.coerce.number()
    .min(0, 'Offset must be non-negative')
    .optional()
    .default(0)
}).strict();

export const reorderTasksSchema = z.object({
  order: z.array(z.coerce.number().positive('Task IDs must be positive'))
    .min(1, 'At least one task ID required')
});
