import type { DashboardOverviewDTO } from '@/lib/types/generated/DashboardOverviewDTO';
import type { DashboardDriverSummaryDTO } from '@/lib/types/generated/DashboardDriverSummaryDTO';
import type { DashboardRaceSummaryDTO } from '@/lib/types/generated/DashboardRaceSummaryDTO';
import type { DashboardLeagueStandingSummaryDTO } from '@/lib/types/generated/DashboardLeagueStandingSummaryDTO';
import type { DashboardFeedItemSummaryDTO } from '@/lib/types/generated/DashboardFeedItemSummaryDTO';
import type { DashboardFriendSummaryDTO } from '@/lib/types/generated/DashboardFriendSummaryDTO';

export class DashboardDriverSummaryViewModel {
  constructor(private readonly dto: DashboardDriverSummaryDTO) {}

  get id(): string {
    return this.dto.id;
  }

  get name(): string {
    return this.dto.name;
  }

  get avatarUrl(): string {
    return this.dto.avatarUrl || '';
  }

  get country(): string {
    return this.dto.country;
  }

  get totalRaces(): number {
    return this.dto.totalRaces;
  }

  get wins(): number {
    return this.dto.wins;
  }

  get podiums(): number {
    return this.dto.podiums;
  }

  get rating(): number {
    return this.dto.rating ?? 0;
  }

  get globalRank(): number {
    return this.dto.globalRank ?? 0;
  }

  get consistency(): number {
    return this.dto.consistency ?? 0;
  }
}

export class DashboardRaceSummaryViewModel {
  constructor(private readonly dto: DashboardRaceSummaryDTO) {}

  get id(): string {
    return this.dto.id;
  }

  get leagueId(): string {
    return (this.dto as any).leagueId ?? '';
  }

  get leagueName(): string {
    return (this.dto as any).leagueName ?? '';
  }

  get track(): string {
    return this.dto.track;
  }

  get car(): string {
    return this.dto.car;
  }

  get scheduledAt(): Date {
    return new Date(this.dto.scheduledAt);
  }

  get status(): string {
    return this.dto.status;
  }

  get isMyLeague(): boolean {
    return this.dto.isMyLeague;
  }
}

export class DashboardLeagueStandingSummaryViewModel {
  constructor(private readonly dto: DashboardLeagueStandingSummaryDTO) {}

  get leagueId(): string {
    return this.dto.leagueId;
  }

  get leagueName(): string {
    return this.dto.leagueName;
  }

  get position(): number {
    return this.dto.position;
  }

  get points(): number {
    return this.dto.points;
  }

  get totalDrivers(): number {
    return this.dto.totalDrivers;
  }
}

export class DashboardFeedItemSummaryViewModel {
  constructor(private readonly dto: DashboardFeedItemSummaryDTO) {}

  get id(): string {
    return this.dto.id;
  }

  get type(): string {
    return this.dto.type;
  }

  get headline(): string {
    return this.dto.headline;
  }

  get body(): string | undefined {
    return this.dto.body;
  }

  get timestamp(): Date {
    return new Date(this.dto.timestamp);
  }

  get ctaHref(): string | undefined {
    return this.dto.ctaHref;
  }

  get ctaLabel(): string | undefined {
    return this.dto.ctaLabel;
  }
}

export class DashboardFriendSummaryViewModel {
  constructor(private readonly dto: DashboardFriendSummaryDTO) {}

  get id(): string {
    return this.dto.id;
  }

  get name(): string {
    return this.dto.name;
  }

  get avatarUrl(): string {
    return this.dto.avatarUrl || '';
  }

  get country(): string {
    return this.dto.country;
  }
}

export class DashboardOverviewViewModel {
  constructor(private readonly dto: DashboardOverviewDTO) {}

  get currentDriver(): DashboardDriverSummaryViewModel {
    // DTO uses optional property; enforce a consistent object for the UI
    return new DashboardDriverSummaryViewModel(
      (this.dto as any).currentDriver ?? {
        id: '',
        name: '',
        country: '',
        avatarUrl: '',
        totalRaces: 0,
        wins: 0,
        podiums: 0,
      },
    );
  }

  get nextRace(): DashboardRaceSummaryViewModel | null {
    const nextRace = (this.dto as any).nextRace;
    return nextRace ? new DashboardRaceSummaryViewModel(nextRace) : null;
  }

  get upcomingRaces(): DashboardRaceSummaryViewModel[] {
    const upcomingRaces = (this.dto as any).upcomingRaces ?? [];
    return upcomingRaces.map((r: any) => new DashboardRaceSummaryViewModel(r));
  }

  get leagueStandings(): DashboardLeagueStandingSummaryViewModel[] {
    const leagueStandings = (this.dto as any).leagueStandingsSummaries ?? (this.dto as any).leagueStandings ?? [];
    return leagueStandings.map((s: any) => new DashboardLeagueStandingSummaryViewModel(s));
  }

  get feedItems(): DashboardFeedItemSummaryViewModel[] {
    const feedItems = (this.dto as any).feedSummary?.items ?? (this.dto as any).feedItems ?? [];
    return feedItems.map((i: any) => new DashboardFeedItemSummaryViewModel(i));
  }

  get friends(): DashboardFriendSummaryViewModel[] {
    const friends = (this.dto as any).friends ?? [];
    return friends.map((f: any) => new DashboardFriendSummaryViewModel(f));
  }

  get activeLeaguesCount(): number {
    return (this.dto as any).activeLeaguesCount ?? 0;
  }
}

export {
  DashboardDriverSummaryViewModel as DriverViewModel,
  DashboardRaceSummaryViewModel as RaceViewModel,
  DashboardLeagueStandingSummaryViewModel as LeagueStandingViewModel,
  DashboardFriendSummaryViewModel as FriendViewModel,
};
