import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsArray, IsBoolean, IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator';

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

  @ApiProperty()
  @IsString()
  name!: string;

  @ApiProperty()
  @IsString()
  country!: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  avatarUrl?: string | null;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  category?: string | null;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsNumber()
  rating?: number | null;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsNumber()
  globalRank?: number | null;

  @ApiProperty()
  @IsNumber()
  totalRaces!: number;

  @ApiProperty()
  @IsNumber()
  wins!: number;

  @ApiProperty()
  @IsNumber()
  podiums!: number;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsNumber()
  consistency?: number | null;
}

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

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  leagueId?: string | null;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  leagueName?: string | null;

  @ApiProperty()
  @IsString()
  track!: string;

  @ApiProperty()
  @IsString()
  car!: string;

  @ApiProperty()
  @IsString()
  scheduledAt!: string;

  @ApiProperty()
  @IsString()
  status!: 'scheduled' | 'running' | 'completed' | 'cancelled';

  @ApiProperty()
  @IsBoolean()
  isMyLeague!: boolean;
}

export class DashboardRecentResultDTO {
  @ApiProperty()
  @IsString()
  raceId!: string;

  @ApiProperty()
  @IsString()
  raceName!: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  leagueId?: string | null;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  leagueName?: string | null;

  @ApiProperty()
  @IsString()
  finishedAt!: string;

  @ApiProperty()
  @IsNumber()
  position!: number;

  @ApiProperty()
  @IsNumber()
  incidents!: number;
}

export class DashboardLeagueStandingSummaryDTO {
  @ApiProperty()
  @IsString()
  leagueId!: string;

  @ApiProperty()
  @IsString()
  leagueName!: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsNumber()
  position?: number | null;

  @ApiProperty()
  @IsNumber()
  totalDrivers!: number;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsNumber()
  points?: number | null;
}

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

  @ApiProperty()
  @IsString()
  type!: string;

  @ApiProperty()
  @IsString()
  headline!: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  body?: string;

  @ApiProperty()
  @IsString()
  timestamp!: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  ctaLabel?: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  ctaHref?: string;
}

export class DashboardFeedSummaryDTO {
  @ApiProperty()
  @IsNumber()
  notificationCount!: number;

  @ApiProperty({ type: [DashboardFeedItemSummaryDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardFeedItemSummaryDTO)
  items!: DashboardFeedItemSummaryDTO[];
}

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

  @ApiProperty()
  @IsString()
  name!: string;

  @ApiProperty()
  @IsString()
  country!: string;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @IsString()
  avatarUrl?: string | null;
}

export class DashboardOverviewDTO {
  @ApiProperty({ nullable: true })
  @IsOptional()
  @ValidateNested()
  @Type(() => DashboardDriverSummaryDTO)
  currentDriver?: DashboardDriverSummaryDTO | null;

  @ApiProperty({ type: [DashboardRaceSummaryDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardRaceSummaryDTO)
  myUpcomingRaces!: DashboardRaceSummaryDTO[];

  @ApiProperty({ type: [DashboardRaceSummaryDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardRaceSummaryDTO)
  otherUpcomingRaces!: DashboardRaceSummaryDTO[];

  @ApiProperty({ type: [DashboardRaceSummaryDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardRaceSummaryDTO)
  upcomingRaces!: DashboardRaceSummaryDTO[];

  @ApiProperty()
  @IsNumber()
  activeLeaguesCount!: number;

  @ApiProperty({ nullable: true })
  @IsOptional()
  @ValidateNested()
  @Type(() => DashboardRaceSummaryDTO)
  nextRace?: DashboardRaceSummaryDTO | null;

  @ApiProperty({ type: [DashboardRecentResultDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardRecentResultDTO)
  recentResults!: DashboardRecentResultDTO[];

  @ApiProperty({ type: [DashboardLeagueStandingSummaryDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardLeagueStandingSummaryDTO)
  leagueStandingsSummaries!: DashboardLeagueStandingSummaryDTO[];

  @ApiProperty()
  @ValidateNested()
  @Type(() => DashboardFeedSummaryDTO)
  feedSummary!: DashboardFeedSummaryDTO;

  @ApiProperty({ type: [DashboardFriendSummaryDTO] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => DashboardFriendSummaryDTO)
  friends!: DashboardFriendSummaryDTO[];
}