/**
 * Corrective Action DTOs - Data Transfer Objects para Acciones Correctivas (CAPA)
 *
 * Representa acciones correctivas y preventivas asociadas a no conformidades.
 *
 * @module Quality (MAI-009)
 */

import {
  IsString,
  IsUUID,
  IsOptional,
  IsEnum,
  IsNumber,
  IsDateString,
  IsBoolean,
  MaxLength,
  MinLength,
  Min,
} from 'class-validator';

/**
 * Tipo de accion correctiva
 */
export enum ActionType {
  CORRECTIVE = 'corrective',
  PREVENTIVE = 'preventive',
  IMPROVEMENT = 'improvement',
}

/**
 * Estado de la accion correctiva
 */
export enum ActionStatus {
  PENDING = 'pending',
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  VERIFIED = 'verified',
}

/**
 * DTO para crear una nueva accion correctiva
 */
export class CreateCorrectiveActionDto {
  @IsUUID()
  nonConformityId: string;

  @IsEnum(ActionType)
  actionType: ActionType;

  @IsString()
  @MinLength(10)
  @MaxLength(2000)
  description: string;

  @IsUUID()
  responsibleId: string;

  @IsDateString()
  dueDate: string;
}

/**
 * DTO para actualizar una accion correctiva existente
 */
export class UpdateCorrectiveActionDto {
  @IsOptional()
  @IsEnum(ActionType)
  actionType?: ActionType;

  @IsOptional()
  @IsString()
  @MinLength(10)
  @MaxLength(2000)
  description?: string;

  @IsOptional()
  @IsUUID()
  responsibleId?: string;

  @IsOptional()
  @IsDateString()
  dueDate?: string;

  @IsOptional()
  @IsEnum(ActionStatus)
  status?: ActionStatus;
}

/**
 * DTO para completar una accion correctiva
 */
export class CompleteCorrectiveActionDto {
  @IsString()
  @MinLength(10)
  @MaxLength(2000)
  completionNotes: string;
}

/**
 * DTO para verificar efectividad de una accion correctiva
 */
export class VerifyCorrectiveActionDto {
  @IsBoolean()
  effectivenessVerified: boolean;

  @IsOptional()
  @IsString()
  @MaxLength(2000)
  verificationNotes?: string;
}

/**
 * DTO para filtrar acciones correctivas en listados
 */
export class CorrectiveActionFiltersDto {
  @IsOptional()
  @IsUUID()
  nonConformityId?: string;

  @IsOptional()
  @IsUUID()
  responsibleId?: string;

  @IsOptional()
  @IsEnum(ActionType)
  actionType?: ActionType;

  @IsOptional()
  @IsEnum(ActionStatus)
  status?: ActionStatus;

  @IsOptional()
  @IsBoolean()
  effectivenessVerified?: boolean;

  @IsOptional()
  @IsDateString()
  dueDateFrom?: string;

  @IsOptional()
  @IsDateString()
  dueDateTo?: string;

  @IsOptional()
  @IsDateString()
  createdFrom?: string;

  @IsOptional()
  @IsDateString()
  createdTo?: string;

  @IsOptional()
  @IsString()
  search?: string;

  @IsOptional()
  @IsNumber()
  @Min(1)
  page?: number;

  @IsOptional()
  @IsNumber()
  @Min(1)
  limit?: number;

  @IsOptional()
  @IsString()
  sortBy?: string;

  @IsOptional()
  @IsEnum(['ASC', 'DESC'])
  sortOrder?: 'ASC' | 'DESC';
}

/**
 * DTO de respuesta para una accion correctiva
 */
export class CorrectiveActionResponseDto {
  id: string;
  tenantId: string;
  nonConformityId: string;
  nonConformity?: {
    id: string;
    ncNumber: string;
    category: string;
    severity: string;
  };
  actionType: ActionType;
  description: string;
  responsibleId: string;
  responsible?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  dueDate: Date;
  status: ActionStatus;
  completedAt?: Date;
  completionNotes?: string;
  verifiedAt?: Date;
  verifiedById?: string;
  verifiedBy?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  effectivenessVerified: boolean;
  createdAt: Date;
  createdById?: string;
  createdBy?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  updatedAt: Date;
  updatedById?: string;
}
