/**
 * Checklist DTOs - Data Transfer Objects para Checklists de Calidad
 *
 * Representa templates de inspeccion de calidad.
 *
 * @module Quality (MAI-009)
 */

import {
  IsString,
  IsUUID,
  IsOptional,
  IsEnum,
  IsNumber,
  IsBoolean,
  IsArray,
  ValidateNested,
  MaxLength,
  MinLength,
  Min,
} from 'class-validator';
import { Type } from 'class-transformer';

/**
 * Etapa de construccion del checklist
 */
export enum ChecklistStage {
  FOUNDATION = 'foundation',
  STRUCTURE = 'structure',
  INSTALLATIONS = 'installations',
  FINISHES = 'finishes',
  DELIVERY = 'delivery',
  CUSTOM = 'custom',
}

/**
 * DTO para crear un item de checklist
 */
export class CreateChecklistItemDto {
  @IsNumber()
  @Min(1)
  sequenceNumber: number;

  @IsString()
  @MaxLength(100)
  category: string;

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

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

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

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

  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

/**
 * DTO para actualizar un item de checklist
 */
export class UpdateChecklistItemDto {
  @IsOptional()
  @IsNumber()
  @Min(1)
  sequenceNumber?: number;

  @IsOptional()
  @IsString()
  @MaxLength(100)
  category?: string;

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

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

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

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

  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

/**
 * DTO para crear un nuevo checklist
 */
export class CreateChecklistDto {
  @IsString()
  @MinLength(2)
  @MaxLength(30)
  code: string;

  @IsString()
  @MinLength(3)
  @MaxLength(255)
  name: string;

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

  @IsEnum(ChecklistStage)
  stage: ChecklistStage;

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

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

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateChecklistItemDto)
  items?: CreateChecklistItemDto[];
}

/**
 * DTO para actualizar un checklist existente
 */
export class UpdateChecklistDto {
  @IsOptional()
  @IsString()
  @MinLength(2)
  @MaxLength(30)
  code?: string;

  @IsOptional()
  @IsString()
  @MinLength(3)
  @MaxLength(255)
  name?: string;

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

  @IsOptional()
  @IsEnum(ChecklistStage)
  stage?: ChecklistStage;

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

  @IsOptional()
  @IsBoolean()
  isActive?: boolean;
}

/**
 * DTO para filtrar checklists en listados
 */
export class ChecklistFiltersDto {
  @IsOptional()
  @IsEnum(ChecklistStage)
  stage?: ChecklistStage;

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

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

  @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 un item de checklist
 */
export class ChecklistItemResponseDto {
  id: string;
  tenantId: string;
  checklistId: string;
  sequenceNumber: number;
  category: string;
  description: string;
  isCritical: boolean;
  requiresPhoto: boolean;
  acceptanceCriteria?: string;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
}

/**
 * DTO de respuesta para un checklist
 */
export class ChecklistResponseDto {
  id: string;
  tenantId: string;
  code: string;
  name: string;
  description?: string;
  stage: ChecklistStage;
  prototypeId?: string;
  isActive: boolean;
  version: number;
  itemsCount?: number;
  items?: ChecklistItemResponseDto[];
  inspectionsCount?: number;
  createdAt: Date;
  createdById?: string;
  createdBy?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  updatedAt: Date;
  updatedById?: string;
  deletedAt?: Date;
  deletedById?: string;
}
