import { Type } from 'class-transformer';
import { IsArray, IsDefined, IsIn, IsNumber, IsObject, IsOptional, IsString, ValidateNested } from 'class-validator';

class RiskReasonDto {
  @IsString()
  key!: string;

  @IsString()
  label!: string;

  @IsIn(['low', 'medium', 'high', 'critical'])
  severity!: 'low' | 'medium' | 'high' | 'critical';

  @IsString()
  detail!: string;
}

class SilentRiskDto {
  @IsString()
  key!: string;

  @IsOptional()
  @IsNumber()
  days?: number;

  @IsString()
  detail!: string;
}

class RiskDailyEvidenceDto {
  @IsArray()
  @IsString({ each: true })
  todayDeliveries!: string[];

  @IsArray()
  @IsString({ each: true })
  todayProgress!: string[];

  @IsArray()
  @IsString({ each: true })
  todayCreations!: string[];

  @IsArray()
  @IsString({ each: true })
  todayRisks!: string[];

  @IsArray()
  @IsString({ each: true })
  progressNotes!: string[];

  @IsArray()
  @IsString({ each: true })
  needsProgressItems!: string[];

  @IsNumber()
  recentActivityCount!: number;

  @IsNumber()
  totalActivityCount!: number;

  @IsNumber()
  todayActualHours!: number;

  @IsOptional()
  @IsString()
  lastActivityAt?: string;
}

class RiskSignalsDto {
  @IsNumber()
  unfinishedCount!: number;

  @IsNumber()
  openBugCount!: number;

  @IsNumber()
  criticalBugCount!: number;

  @IsNumber()
  failedTestCount!: number;

  @IsNumber()
  blockedCount!: number;

  @IsNumber()
  silentRiskCount!: number;

  @IsOptional()
  @IsNumber()
  daysToExpectedRelease?: number;
}

export class RiskInterpretDto {
  @IsString()
  versionId!: string;

  @IsString()
  versionName!: string;

  @IsOptional()
  @IsString()
  productName?: string;

  @IsOptional()
  @IsString()
  projectName?: string;

  @IsNumber()
  riskScore!: number;

  @IsIn(['on_track', 'attention', 'at_risk', 'likely_delayed', 'blocked'])
  riskLevel!: 'on_track' | 'attention' | 'at_risk' | 'likely_delayed' | 'blocked';

  @IsOptional()
  @IsString()
  expectedReleaseDate?: string;

  @IsOptional()
  @IsString()
  forecastReleaseDate?: string;

  @IsNumber()
  delayDays!: number;

  @IsNumber()
  confidence!: number;

  @IsDefined()
  @IsObject()
  @ValidateNested()
  @Type(() => RiskSignalsDto)
  signals!: RiskSignalsDto;

  @IsString()
  trendSummary!: string;

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => RiskReasonDto)
  reasons!: RiskReasonDto[];

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => SilentRiskDto)
  silentRisks!: SilentRiskDto[];

  @IsDefined()
  @IsObject()
  @ValidateNested()
  @Type(() => RiskDailyEvidenceDto)
  dailyEvidence!: RiskDailyEvidenceDto;
}
