import { Driver } from '@core/racing/domain/entities/Driver';
import { League } from '@core/racing/domain/entities/League';
import { Race } from '@core/racing/domain/entities/Race';
import { Result } from '@core/racing/domain/entities/Result';
import { Standing } from '@core/racing/domain/entities/Standing';

import type { FeedItem } from '@core/social/domain/types/FeedItem';
import type { SocialFriendSummary } from '@core/social/application/types/SocialUser';

import { faker } from '../../helpers/faker/faker';

import {
  createDrivers,
  createLeagues,
  createTeams,
  createMemberships,
  createRaces,
  createResults,
  createStandings,
  createFriendships,
  type RacingMembership,
  type Friendship,
  type DemoTeamDTO,
} from './RacingSeedCore';
import {
  DEMO_SPONSORS,
  createSeasonSponsorships,
  createSponsorshipPricings,
  createSponsorshipRequests,
  type DemoSponsorDTO,
  type DemoSeasonSponsorshipDTO,
  type DemoSponsorshipRequestDTO,
  type DemoSponsorshipPricingDTO,
} from './RacingSponsorshipSeed';
import {
  createFeedEvents,
  buildFriends,
  buildTopLeagues,
  buildUpcomingRaces,
  buildLatestResults,
  type RaceWithResultsDTO,
} from './RacingFeedSeed';

/**
 * Aggregated racing seed data used by the website DI container
 * and other demo infrastructure.
 */
export type RacingSeedData = {
  drivers: Driver[];
  leagues: League[];
  races: Race[];
  results: Result[];
  standings: Standing[];
  memberships: RacingMembership[];
  friendships: Friendship[];
  feedEvents: FeedItem[];
  teams: DemoTeamDTO[];
  sponsors: DemoSponsorDTO[];
  seasonSponsorships: DemoSeasonSponsorshipDTO[];
  sponsorshipRequests: DemoSponsorshipRequestDTO[];
  sponsorshipPricings: DemoSponsorshipPricingDTO[];
};

/**
 * Create the full static racing seed from the smaller core/sponsorship/feed modules.
 */
export function createStaticRacingSeed(seed: number): RacingSeedData {
  faker.seed(seed);

  const drivers = createDrivers(96);
  const leagues = createLeagues(drivers.slice(0, 12).map((d) => d.id));
  const teams = createTeams(leagues);
  const memberships = createMemberships(drivers, leagues, teams);
  const races = createRaces(leagues);
  const results = createResults(drivers, races);
  const friendships = createFriendships(drivers);
  const feedEvents = createFeedEvents(drivers, leagues, races, friendships);
  const standings = createStandings(leagues, results);
  const sponsors = DEMO_SPONSORS;
  const seasonSponsorships = createSeasonSponsorships(leagues, sponsors);
  const sponsorshipPricings = createSponsorshipPricings(leagues, teams, drivers, races);
  const sponsorshipRequests = createSponsorshipRequests(sponsors, leagues, teams, drivers, races);

  return {
    drivers,
    leagues,
    races,
    results,
    standings,
    memberships,
    friendships,
    feedEvents,
    teams,
    sponsors,
    seasonSponsorships,
    sponsorshipRequests,
    sponsorshipPricings,
  };
}

/**
 * Singleton seed used by website demo helpers.
 *
 * Alpha demo dataset (deterministic, in-memory only):
 * - 90+ drivers across multiple leagues
 * - Leagues with precomputed races, results and standings
 * - Team memberships and friendships forming social “circles”
 * - Feed events referencing real driver, league, race and team IDs
 */
const staticSeed = createStaticRacingSeed(42);

export const drivers = staticSeed.drivers;
export const leagues = staticSeed.leagues;
export const races = staticSeed.races;
export const results = staticSeed.results;
export const standings = staticSeed.standings;
export const teams = staticSeed.teams;
export const memberships = staticSeed.memberships;
export const friendships = staticSeed.friendships;
export const feedEvents = staticSeed.feedEvents;
export const sponsors = staticSeed.sponsors;
export const seasonSponsorships = staticSeed.seasonSponsorships;
export const sponsorshipRequests = staticSeed.sponsorshipRequests;
export const sponsorshipPricings = staticSeed.sponsorshipPricings;

/**
 * Derived friend DTOs for UI consumption.
 * This preserves the previous demo-data `friends` shape.
 */
export const friends: SocialFriendSummary[] = buildFriends(staticSeed.drivers, staticSeed.memberships);

/**
 * Top leagues with banner URLs for UI.
 */
export const topLeagues = buildTopLeagues(leagues);

/**
 * Re-export RaceWithResultsDTO and helpers for latest/upcoming races.
 */
export type { RaceWithResultsDTO } from './RacingFeedSeed';

export function getUpcomingRaces(limit?: number): readonly Race[] {
  return buildUpcomingRaces(races, limit);
}

export function getLatestResults(limit?: number): readonly RaceWithResultsDTO[] {
  return buildLatestResults(races, results, drivers, limit);
}

/**
 * Demo league archetype helper for seeding structure and scoring.
 * Kept here as the small, focused definition used by DI.
 */
export type DemoLeagueArchetype =
  | {
      id: 'sprint-series';
      name: 'GridPilot Sprint Series';
      structure: { mode: 'solo'; maxDrivers: 24 };
      scoringPresetId: 'sprint-main-driver';
    }
  | {
      id: 'endurance-cup';
      name: 'GridPilot Endurance Cup';
      structure: { mode: 'fixedTeams'; maxTeams: 12; driversPerTeam: 2 };
      scoringPresetId: 'endurance-main-double';
    }
  | {
      id: 'club-ladder';
      name: 'GridPilot Club Ladder';
      structure: { mode: 'solo'; maxDrivers: 40 };
      scoringPresetId: 'club-default';
    };

export function getDemoLeagueArchetypeByName(
  leagueName: string,
): DemoLeagueArchetype | undefined {
  switch (leagueName) {
    case 'GridPilot Sprint Series':
      return {
        id: 'sprint-series',
        name: 'GridPilot Sprint Series',
        structure: { mode: 'solo', maxDrivers: 24 },
        scoringPresetId: 'sprint-main-driver',
      };
    case 'GridPilot Endurance Cup':
      return {
        id: 'endurance-cup',
        name: 'GridPilot Endurance Cup',
        structure: { mode: 'fixedTeams', maxTeams: 12, driversPerTeam: 2 },
        scoringPresetId: 'endurance-main-double',
      };
    case 'GridPilot Club Ladder':
      return {
        id: 'club-ladder',
        name: 'GridPilot Club Ladder',
        structure: { mode: 'solo', maxDrivers: 40 },
        scoringPresetId: 'club-default',
      };
    default:
      return undefined;
  }
}