/**
 * Inspection DTOs - Data Transfer Objects para Inspecciones de Calidad
 *
 * Representa una inspeccion de calidad realizada en obra.
 *
 * @module Quality (MAI-009)
 */

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

/**
 * Estado de la inspeccion
 */
export enum InspectionStatus {
  PENDING = 'pending',
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  APPROVED = 'approved',
  REJECTED = 'rejected',
}

/**
 * DTO para crear una nueva inspeccion
 */
export class CreateInspectionDto {
  @IsUUID()
  checklistId: string;

  @IsUUID()
  loteId: string;

  @IsDateString()
  inspectionDate: string;

  @IsUUID()
  inspectorId: string;

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

/**
 * DTO para actualizar una inspeccion existente
 */
export class UpdateInspectionDto {
  @IsOptional()
  @IsUUID()
  checklistId?: string;

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

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

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

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

  @IsOptional()
  @IsNumber()
  @Min(0)
  totalItems?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  passedItems?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  failedItems?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(100)
  passRate?: number;

  @IsOptional()
  @IsString()
  @MaxLength(2000)
  notes?: string;

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

/**
 * DTO para aprobar una inspeccion
 */
export class ApproveInspectionDto {
  @IsOptional()
  @IsString()
  @MaxLength(500)
  notes?: string;
}

/**
 * DTO para rechazar una inspeccion
 */
export class RejectInspectionDto {
  @IsString()
  @MaxLength(2000)
  rejectionReason: string;
}

/**
 * DTO para filtrar inspecciones en listados
 */
export class InspectionFiltersDto {
  @IsOptional()
  @IsEnum(InspectionStatus)
  status?: InspectionStatus;

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

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

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

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

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

  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(100)
  minPassRate?: number;

  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(100)
  maxPassRate?: number;

  @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 inspeccion
 */
export class InspectionResponseDto {
  id: string;
  tenantId: string;
  checklistId: string;
  checklist?: {
    id: string;
    code: string;
    name: string;
    stage: string;
  };
  loteId: string;
  inspectionNumber: string;
  inspectionDate: Date;
  inspectorId: string;
  inspector?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  status: InspectionStatus;
  totalItems: number;
  passedItems: number;
  failedItems: number;
  passRate?: number;
  completedAt?: Date;
  approvedById?: string;
  approvedBy?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  approvedAt?: Date;
  notes?: string;
  rejectionReason?: string;
  resultsCount?: number;
  nonConformitiesCount?: number;
  createdAt: Date;
  createdById?: string;
  createdBy?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  updatedAt: Date;
  updatedById?: string;
}
