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

import { AuditMode, CertificationType, ClientGroup } from '../entities/audit-request.entity';

// 🆕 One attendee (name + designation + optional email/contact) for the attendance sheet.
export class AuditeeDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(150)
  name: string;

  @IsOptional()
  @IsString()
  @MaxLength(150)
  designation?: string;

  @IsOptional()
  @IsEmail()
  email?: string;

  @IsOptional()
  @IsString()
  @MaxLength(30)
  contact?: string;
}

/**
 * DTO for the marketing "New Audit Request" form.
 * Maps directly to the audit_requests table columns.
 */
export class CreateAuditRequestDto {
  // ── Client & auditee ──────────────────────────────────────────────
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  company_id?: number;
  // 🆕 ADD THESE:
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  company_branch_id?: number;

  @IsOptional()
  @IsString()
  @MaxLength(255)
  branch_name?: string;

  // ── Company snapshot from the clients DB search ───────────────────
  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  company_name: string;

  @IsOptional()
  @IsString()
  @MaxLength(20)
  company_source?: string;   // 'Client' | 'Surveillance'

  @IsEnum(ClientGroup)
  client_group: ClientGroup;   // QRS | TQS — selected on the form

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  client_ref_id?: number;

  @IsString()
  @IsNotEmpty()
  @MaxLength(150)
  auditee_name: string;

  @IsString()
  @IsNotEmpty()
  @MaxLength(30)
  auditee_contact: string;

  @IsEmail()
  @MaxLength(150)
  auditee_email: string;

  // 🆕 Attendance list — multiple auditees (name + designation)
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => AuditeeDto)
  auditees?: AuditeeDto[];

  // ── Audit scope ───────────────────────────────────────────────────
  @IsArray()
  @ArrayMinSize(1, { message: 'At least one standard must be selected' })
  @IsInt({ each: true })
  @Type(() => Number)
  standard_ids: number[];

  @IsEnum(CertificationType)
  certification_type: CertificationType;

  @IsString()
  @IsNotEmpty()
  @MaxLength(50)
  accreditation: string;

  // 🆕 ── Certification details ──────────────────────────────────────
  // Carried into the inquiry on "Proceed to Inquiry" and synced to
  // companies.scope_of_work.
  @IsOptional()
  @IsString()
  @MaxLength(5000)
  scope_of_work?: string;

  @IsOptional()
  @IsString()
  @MaxLength(120)
  previous_cert_no?: string;

  // ── Scheduling preference ─────────────────────────────────────────
  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/, {
    message: 'proposed_date must be in YYYY-MM-DD format',
  })
  proposed_date: string;

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

  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  location: string;

  @IsEnum(AuditMode)
  mode: AuditMode;

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