import { MediaReference } from '@core/domain/media/MediaReference';
import { League } from '@core/racing/domain/entities/League';
import { LeagueRepository } from '@core/racing/domain/repositories/LeagueRepository';
import { Logger } from '@core/shared/domain';

export class InMemoryLeagueRepository implements LeagueRepository {
  private leagues: Map<string, League> = new Map();
  private readonly logger: Logger;

  constructor(logger: Logger) {
    this.logger = logger;
    this.leagues = new Map();

    this.logger.info('InMemoryLeagueRepository initialized');
  }

  clear(): void {
    this.leagues.clear();
  }

  async findById(id: string): Promise<League | null> {
    this.logger.debug(`Attempting to find league with ID: ${id}.`);
    try {
      const league = this.leagues.get(id) ?? null;
      if (league) {
        this.logger.info(`Successfully found league with ID: ${id}.`);
      } else {
        this.logger.warn(`League with ID: ${id} not found.`);
      }
      return league;
    } catch (error) {
      this.logger.error(`Error finding league by ID ${id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByOwnerId(ownerId: string): Promise<League[]> {
    this.logger.debug(`Attempting to find leagues by owner ID: ${ownerId}.`);
    try {
      const ownedLeagues = Array.from(this.leagues.values()).filter(league => league.ownerId.toString() === ownerId);
      this.logger.info(`Successfully found ${ownedLeagues.length} leagues for owner ID: ${ownerId}.`);
      return ownedLeagues;
    } catch (error) {
      this.logger.error(`Error finding leagues by owner ID ${ownerId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async searchByName(name: string): Promise<League[]> {
    this.logger.debug(`Attempting to search leagues by name query: ${name}.`);
    try {
      const matchingLeagues = Array.from(this.leagues.values()).filter(league =>
        league.name.toString().toLowerCase().includes(name.toLowerCase()),
      );
      this.logger.info(`Successfully found ${matchingLeagues.length} leagues matching search query: ${name}.`);
      return matchingLeagues;
    } catch (error) {
      this.logger.error(`Error searching leagues by name query ${name}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findAll(): Promise<League[]> {
    this.logger.debug('Attempting to find all leagues.');
    try {
      const leagues = Array.from(this.leagues.values());
      this.logger.info(`Successfully found ${leagues.length} leagues.`);
      return leagues;
    } catch (error) {
      this.logger.error('Error finding all leagues:', error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async countAll(): Promise<number> {
    return this.leagues.size;
  }

  async create(league: League): Promise<League> {
    this.logger.debug(`Attempting to create league: ${league.id}.`);
    try {
      if (await this.exists(league.id.toString())) {
        this.logger.warn(`League with ID ${league.id} already exists; creation aborted.`);
        throw new Error(`League with ID ${league.id} already exists`);
      }

      this.leagues.set(league.id.toString(), league);
      this.logger.info(`League ${league.id} created successfully.`);
      return league;
    } catch (error) {
      this.logger.error(`Error creating league ${league.id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async update(league: League): Promise<League> {
    this.logger.debug(`Attempting to update league with ID: ${league.id}.`);
    try {
      if (!(await this.exists(league.id.toString()))) {
        this.logger.warn(`League with ID ${league.id} not found for update; update aborted.`);
        throw new Error(`League with ID ${league.id} not found`);
      }

      this.leagues.set(league.id.toString(), league);
      this.logger.info(`League ${league.id} updated successfully.`);
      return league;
    } catch (error) {
      this.logger.error(`Error updating league ${league.id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async delete(id: string): Promise<void> {
    this.logger.debug(`Attempting to delete league with ID: ${id}.`);
    try {
      if (!(await this.exists(id))) {
        this.logger.warn(`League with ID ${id} not found for deletion; deletion aborted.`);
        throw new Error(`League with ID ${id} not found`);
      }

      this.leagues.delete(id);
      this.logger.info(`League ${id} deleted successfully.`);
    } catch (error) {
      this.logger.error(`Error deleting league ${id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async exists(id: string): Promise<boolean> {
    this.logger.debug(`Checking existence of league with ID: ${id}.`);
    try {
      const exists = this.leagues.has(id);
      this.logger.info(`League with ID ${id} existence check: ${exists}.`);
      return exists;
    } catch (error) {
      this.logger.error(`Error checking existence of league with ID ${id}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  // Serialization methods for persistence
  serialize(league: League): Record<string, unknown> {
    return {
      id: league.id.toString(),
      name: league.name.toString(),
      description: league.description.toString(),
      ownerId: league.ownerId.toString(),
      settings: league.settings,
      category: league.category ?? null,
      createdAt: league.createdAt.toDate().toISOString(),
      participantCount: league.getParticipantCount(),
      socialLinks: league.socialLinks
        ? {
            discordUrl: league.socialLinks.discordUrl,
            youtubeUrl: league.socialLinks.youtubeUrl,
            websiteUrl: league.socialLinks.websiteUrl,
          }
        : undefined,
      logoRef: league.logoRef.toJSON(),
    };
  }

  deserialize(data: Record<string, unknown>): League {
    const props: {
      id: string;
      name: string;
      description: string;
      ownerId: string;
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      settings: any;
      category?: string;
      createdAt: Date;
      participantCount: number;
      socialLinks?: {
        discordUrl?: string;
        youtubeUrl?: string;
        websiteUrl?: string;
      };
      logoRef?: MediaReference;
    } = {
      id: data.id as string,
      name: data.name as string,
      description: data.description as string,
      ownerId: data.ownerId as string,
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      settings: data.settings as any,
      createdAt: new Date(data.createdAt as string),
      participantCount: data.participantCount as number,
    };

    if (data.category !== null && data.category !== undefined) {
      props.category = data.category as string;
    }
    if (data.socialLinks !== null && data.socialLinks !== undefined) {
      props.socialLinks = data.socialLinks as {
        discordUrl?: string;
        youtubeUrl?: string;
        websiteUrl?: string;
      };
    }
    if (data.logoRef !== null && data.logoRef !== undefined) {
      props.logoRef = MediaReference.fromJSON(data.logoRef as Record<string, unknown>);
    }

    return League.rehydrate(props);
  }
}