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';
import { AuditeeDto } from './create-audit-request.dto';

export class BatchClientDto {
  // ── Client identity ────────────────────────────────────────────────
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  company_id?: number;

  // 🆕 ADDED: Company branch selection (optional)
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  company_branch_id?: number;

  // 🆕 ADDED: Branch name (if creating new branch)
  @IsOptional()
  @IsString()
  @MaxLength(255)
  branch_name?: string;

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

  @IsOptional()
  @IsString()
  @MaxLength(20)
  company_source?: string;

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

  // ── Auditee (per client) ───────────────────────────────────────────
  @IsString()
  @IsNotEmpty()
  @MaxLength(150)
  auditee_name: string;

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

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

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

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

  @IsEnum(CertificationType)
  certification_type: CertificationType;

  @IsEnum(AuditMode)
  mode: AuditMode;

  // 🆕 ── Certification details (per client) ───────────────────────────
  // Each client has their own scope + previous cert — 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;

  // ── Schedule preference (per client) ───────────────────────────────
  @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;

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

export class CreateBatchAuditRequestDto {
  // ── Shared scope (applies to every client) ─────────────────────────
  @IsEnum(ClientGroup)
  client_group: ClientGroup;

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

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

  // ── Clients ─────────────────────────────────────────────────────────
  @IsArray()
  @ArrayMinSize(1, { message: 'At least one client is required' })
  @ValidateNested({ each: true })
  @Type(() => BatchClientDto)
  clients: BatchClientDto[];
}