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

export enum Sport {
  FOOTBALL = "football",
  BASKETBALL = "basketball",
}

export class OddFilterDto {
  @ApiProperty({ example: "Maç Sonucu" })
  @IsString()
  categoryName: string;

  @ApiProperty({ example: "1" })
  @IsString()
  selectionName: string;

  @ApiProperty({ example: 1.5 })
  value: number;

  @ApiPropertyOptional({ example: 0.1 })
  @IsOptional()
  tolerance?: number = 0.1;
}

export class TeamFilterDto {
  @ApiProperty()
  @IsString()
  id: string;

  @ApiPropertyOptional({ enum: ["home", "away", "any"] })
  @IsOptional()
  @IsString()
  role?: "home" | "away" | "any";
}

export class DateRangeDto {
  @ApiProperty()
  @IsDateString()
  from: string;

  @ApiProperty()
  @IsDateString()
  to: string;
}

export class MatchQueryDto {
  @ApiProperty({ enum: Sport, default: Sport.FOOTBALL })
  @IsEnum(Sport)
  sport: Sport;

  @ApiPropertyOptional({ default: 50 })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  limit?: number = 50;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  leagueId?: string;

  @ApiPropertyOptional({
    description: "Filter by status: LIVE, Finished, etc.",
  })
  @IsOptional()
  @IsString()
  status?: string;

  @ApiPropertyOptional({ description: "Single date filter (YYYY-MM-DD)" })
  @IsOptional()
  @IsDateString()
  date?: string;

  @ApiPropertyOptional({ type: TeamFilterDto })
  @IsOptional()
  @ValidateNested()
  @Type(() => TeamFilterDto)
  team?: TeamFilterDto;

  @ApiPropertyOptional({ type: [OddFilterDto] })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => OddFilterDto)
  odds?: OddFilterDto[];

  @ApiPropertyOptional({ type: DateRangeDto })
  @IsOptional()
  @ValidateNested()
  @Type(() => DateRangeDto)
  dateRange?: DateRangeDto;
}

export class MatchResponseDto {
  @ApiProperty()
  id: string;

  @ApiProperty()
  matchName: string;

  @ApiPropertyOptional()
  matchSlug?: string;

  @ApiProperty()
  mstUtc: number;

  @ApiPropertyOptional()
  status?: string;

  @ApiPropertyOptional()
  state?: string;

  @ApiPropertyOptional()
  scoreHome?: number;

  @ApiPropertyOptional()
  scoreAway?: number;

  @ApiPropertyOptional()
  htScoreHome?: number;

  @ApiPropertyOptional()
  htScoreAway?: number;

  @ApiProperty()
  homeTeamName: string;

  @ApiPropertyOptional()
  homeTeamLogo?: string;

  @ApiProperty()
  awayTeamName: string;

  @ApiPropertyOptional()
  awayTeamLogo?: string;

  @ApiPropertyOptional()
  leagueName?: string;

  @ApiPropertyOptional()
  countryName?: string;

  @ApiPropertyOptional({ type: "array" })
  odds?: any[];
}

export class PaginatedMatchesDto {
  @ApiProperty({ type: [MatchResponseDto] })
  matches: MatchResponseDto[];

  @ApiProperty()
  total: number;

  @ApiProperty()
  page: number;

  @ApiProperty()
  totalPages: number;
}

export class LeagueWithMatchesDto {
  @ApiProperty()
  id: string;

  @ApiProperty()
  name: string;

  @ApiPropertyOptional()
  code?: string;

  @ApiProperty()
  country: {
    id: string;
    name: string;
    flagUrl?: string;
  };

  @ApiProperty()
  sport: Sport;

  @ApiProperty({ type: [MatchResponseDto] })
  matches: MatchResponseDto[];
}

export class ActiveLeagueDto {
  @ApiProperty()
  id: string;

  @ApiProperty()
  name: string;

  @ApiPropertyOptional()
  code?: string;

  @ApiPropertyOptional()
  countryName?: string;

  @ApiPropertyOptional()
  countryFlag?: string;

  @ApiProperty()
  matchCount: number;

  @ApiProperty()
  liveCount: number;
}
