import {
  ArrayMinSize,
  IsArray,
  IsIn,
  IsInt,
  IsOptional,
  IsString,
  MaxLength,
  ValidateIf,
} from 'class-validator';
import { Type } from 'class-transformer';

import {
  ClientGroup,
  LeadStatus,
  LEAD_LOST_REASONS,
  SELECTABLE_CLIENT_GROUPS,
} from '../entities/lead.entity';

export class BulkUpdateLeadDto {
  @IsArray()
  @ArrayMinSize(1)
  @IsInt({ each: true })
  @Type(() => Number)
  ids: number[];

  // 🆕 'client_group' added — bulk-tagging a batch of imported leads into
  // the right group is the main reason this action exists.
  @IsIn(['status', 'reassign', 'client_group'])
  action: 'status' | 'reassign' | 'client_group';

  // 'Converted' intentionally excluded — bulk-converting requires creating a
  // client record, which must go through the dedicated convert flow.
  @ValidateIf((o) => o.action === 'status')
  @IsIn([LeadStatus.NEW, LeadStatus.INTERESTED, LeadStatus.LOST])
  status?: LeadStatus;

  @ValidateIf((o) => o.status === LeadStatus.LOST)
  @IsIn(LEAD_LOST_REASONS)
  lost_reason?: string;

  @ValidateIf((o) => o.action === 'reassign')
  @IsInt()
  @Type(() => Number)
  assigned_to?: number;

  /** Optional handover note, attached to the bulk reassignment notification. */
  @ValidateIf((o) => o.action === 'reassign')
  @IsOptional()
  @IsString()
  @MaxLength(1000)
  note?: string;

  // QRS_B is not offered here either — legacy read-only, same as create.
  @ValidateIf((o) => o.action === 'client_group')
  @IsIn(SELECTABLE_CLIENT_GROUPS as readonly string[], {
    message: `client_group must be one of: ${SELECTABLE_CLIENT_GROUPS.join(', ')}`,
  })
  client_group?: ClientGroup;
}

export class BulkDestroyLeadDto {
  @IsArray()
  @ArrayMinSize(1)
  @IsInt({ each: true })
  @Type(() => Number)
  ids: number[];
}
