import {
  IsArray,
  IsEnum,
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsString,
  Matches,
  MaxLength,
  Min,
} from 'class-validator';
import { Type } from 'class-transformer';

import {
  AuditType,
  AuditMode,
} from '../../audit-schedules/entities/audit-schedule-row.entity';

/**
 * DTO for the coordinator "Schedule Audit" modal.
 * This produces a row in audit_schedule_rows AND a parent audit_schedules
 * row if one doesn't exist for that date+coordinator.
 *
 * Most data (company_id, accreditation, standards, mode) is COPIED from
 * the audit_request being scheduled — only these fields are user input.
 */
export class ScheduleAuditRequestDto {
  // ── Auditor assignment ────────────────────────────────────────────
  @IsInt()
  @Type(() => Number)
  lead_auditor_id: number;

  // 🆕 Additional auditors (Auditor 2, 3, ...) — optional
  @IsOptional()
  @IsArray()
  @IsInt({ each: true })
  @Type(() => Number)
  co_auditor_ids?: number[];

  // ── Confirmed date/time (may differ from proposed_date) ───────────
  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/, {
    message: 'audit_date must be YYYY-MM-DD',
  })
  audit_date: string;

  @IsString()
  @Matches(/^\d{2}:\d{2}(:\d{2})?$/, {
    message: 'audit_time must be HH:MM or HH:MM:SS',
  })
  audit_time: string;

  @IsString()
  @IsNotEmpty()
  @MaxLength(20)
  audit_time_label: string;  // e.g. "11.00AM"

  // ── Audit type — coordinator may refine this ──────────────────────
  // (e.g. marketing said SURVEILLANCE_2, coordinator records as SURVEILLANCE)
  @IsEnum(AuditType)
  audit_type: AuditType;

  @IsOptional()
  @IsString()
  @MaxLength(50)
  audit_stage?: string;  // e.g. "Stage 1", "Stage 2"

  // ── May override the mode that marketing proposed ─────────────────
  @IsOptional()
  @IsEnum(AuditMode)
  audit_mode?: AuditMode;

  // ── Client group (optional, for parent schedule) ──────────────────
  @IsOptional()
  @IsString()
  @MaxLength(100)
  client_group?: string;

  // ── Notes ─────────────────────────────────────────────────────────
  @IsOptional()
  @IsString()
  @MaxLength(2000)
  coordinator_remarks?: string;

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