import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import {
  IsArray,
  IsDateString,
  IsEnum,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  Max,
  Min,
  ValidateNested,
} from "class-validator";
import { Type } from "class-transformer";

// ─── Bulletin Match Item (used in CreateBulletinDto) ───

export class BulletinMatchItemDto {
  @ApiProperty({ example: 1, description: "Sıra numarası (1-15)" })
  @IsInt()
  @Min(1)
  @Max(15)
  matchOrder: number;

  @ApiProperty({ example: "Blackpool" })
  @IsString()
  homeTeamName: string;

  @ApiProperty({ example: "Burton Albion" })
  @IsString()
  awayTeamName: string;

  @ApiPropertyOptional({ example: "İN1" })
  @IsOptional()
  @IsString()
  leagueName?: string;

  @ApiPropertyOptional({ example: "2026-03-28T18:00:00" })
  @IsOptional()
  @IsDateString()
  kickoffTime?: string;

  @ApiPropertyOptional({ description: "Link to existing match ID" })
  @IsOptional()
  @IsString()
  matchId?: string;
}

// ─── Create Bulletin DTO ───

export class CreateBulletinDto {
  @ApiProperty({ example: 333, description: "Game cycle number from API" })
  @IsInt()
  gameCycleNo: number;

  @ApiPropertyOptional({ example: "27-29 Mart" })
  @IsOptional()
  @IsString()
  programName?: string;

  @ApiPropertyOptional({ example: "2025-2026" })
  @IsOptional()
  @IsString()
  season?: string;

  @ApiPropertyOptional({ example: "2026-03-22T10:00:00" })
  @IsOptional()
  @IsDateString()
  payinBeginDate?: string;

  @ApiPropertyOptional({ example: "2026-03-27T20:55:00" })
  @IsOptional()
  @IsDateString()
  payinEndDate?: string;

  @ApiProperty({ type: [BulletinMatchItemDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => BulletinMatchItemDto)
  matches: BulletinMatchItemDto[];
}

// ─── Update Results DTO ───

export class MatchResultDto {
  @ApiProperty({ example: 1, description: "Match order (1-15)" })
  @IsInt()
  @Min(1)
  @Max(15)
  matchOrder: number;

  @ApiProperty({ enum: ["HOME", "DRAW", "AWAY"], example: "HOME" })
  @IsEnum({ HOME: "HOME", DRAW: "DRAW", AWAY: "AWAY" })
  result: "HOME" | "DRAW" | "AWAY";

  @ApiPropertyOptional({ default: false })
  @IsOptional()
  isCancelled?: boolean;

  @ApiPropertyOptional({ enum: ["HOME", "DRAW", "AWAY"] })
  @IsOptional()
  @IsEnum({ HOME: "HOME", DRAW: "DRAW", AWAY: "AWAY" })
  drawResult?: "HOME" | "DRAW" | "AWAY";
}

export class UpdateResultsDto {
  @ApiProperty({ type: [MatchResultDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => MatchResultDto)
  results: MatchResultDto[];

  @ApiPropertyOptional({ description: "15 bilen sayısı" })
  @IsOptional()
  @IsInt()
  winners15?: number;

  @ApiPropertyOptional({ description: "15 bilen ödülü (TL)" })
  @IsOptional()
  @IsNumber()
  prize15?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  winners14?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  prize14?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  winners13?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  prize13?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  winners12?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  prize12?: number;

  @ApiPropertyOptional({ description: "Sonraki haftaya devir" })
  @IsOptional()
  @IsNumber()
  rolloverNext?: number;
}

// ─── Generate Columns DTO ───

export type TotoSelectionType = "1" | "X" | "2";

export class TotoMatchSelection {
  @ApiProperty({ example: 1 })
  @IsInt()
  @Min(1)
  @Max(15)
  matchOrder: number;

  @ApiProperty({
    type: [String],
    example: ["1", "X"],
    description: "Seçimler: 1=Ev, X=Beraberlik, 2=Deplasman",
  })
  @IsArray()
  selections: TotoSelectionType[];
}

export class GenerateColumnsDto {
  @ApiProperty({ description: "Bulletin ID" })
  @IsString()
  bulletinId: string;

  @ApiProperty({ type: [TotoMatchSelection] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => TotoMatchSelection)
  matchSelections: TotoMatchSelection[];

  @ApiPropertyOptional({
    example: "FULL_SYSTEM",
    description: "FULL_SYSTEM | REDUCED_SYSTEM | MANUAL",
  })
  @IsOptional()
  @IsString()
  strategy?: string;

  @ApiPropertyOptional({
    example: 100,
    description: "Max kolon sayısı (reduced system için)",
  })
  @IsOptional()
  @IsInt()
  maxColumns?: number;
}

// ─── Generate AI Prediction DTO ───

export class GenerateSporTotoPredictionDto {
  @ApiProperty({ description: "Bulletin ID" })
  @IsString()
  bulletinId: string;

  @ApiPropertyOptional({
    example: "BALANCED",
    enum: ["CONSERVATIVE", "BALANCED", "AGGRESSIVE", "FORMULA_6PCT"],
    description:
      "CONSERVATIVE(100 col), BALANCED(500), AGGRESSIVE(2500), FORMULA_6PCT(%6 sampling)",
  })
  @IsOptional()
  @IsString()
  strategy?: "CONSERVATIVE" | "BALANCED" | "AGGRESSIVE" | "FORMULA_6PCT";

  @ApiPropertyOptional({
    example: 500,
    description: "Max bütçe (TL). Kolon sayısı buna göre sınırlanır.",
  })
  @IsOptional()
  @IsNumber()
  maxBudget?: number;

  @ApiPropertyOptional({
    example: 200,
    description: "Max kolon sayısı override",
  })
  @IsOptional()
  @IsInt()
  maxColumns?: number;
}

// ─── Evaluate Columns DTO ───

export class EvaluateColumnsDto {
  @ApiProperty({ description: "Bulletin ID" })
  @IsString()
  bulletinId: string;

  @ApiProperty({
    type: [String],
    example: ["11X2X1XX21X1121"],
    description: "Array of 15-char column strings",
  })
  @IsArray()
  @IsString({ each: true })
  columns: string[];
}
