/**
 * Infrastructure Adapter: InMemoryCarRepository
 * 
 * In-memory implementation of ICarRepository.
 * Stores data in Map structure with UUID generation.
 */

import { Car } from '@core/racing/domain/entities/Car';
import { CarClass } from '@core/racing/domain/entities/CarClass';
import { CarLicense } from '@core/racing/domain/entities/CarLicense';
import type { CarRepository } from '@core/racing/domain/repositories/CarRepository';
import type { Logger } from '@core/shared/domain/Logger';
import { v4 as uuidv4 } from 'uuid';

export class InMemoryCarRepository implements CarRepository {
  private cars: Map<string, Car>;
  private readonly logger: Logger;

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

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

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

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

  async findByGameId(gameId: string): Promise<Car[]> {
    this.logger.debug(`Attempting to find cars by game ID: ${gameId}.`);
    try {
      const cars = Array.from(this.cars.values())
        .filter(car => car.gameId.toString() === gameId)
        .sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
      this.logger.info(`Successfully found ${cars.length} cars for game ID: ${gameId}.`);
      return cars;
    } catch (error) {
      this.logger.error(`Error finding cars by game ID ${gameId}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByClass(carClass: CarClass): Promise<Car[]> {
    this.logger.debug(`Attempting to find cars by class: ${carClass}.`);
    try {
      const cars = Array.from(this.cars.values())
        .filter(car => car.carClass.equals(carClass))
        .sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
      this.logger.info(`Successfully found ${cars.length} cars for class: ${carClass}.`);
      return cars;
    } catch (error) {
      this.logger.error(`Error finding cars by class ${carClass}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByLicense(license: CarLicense): Promise<Car[]> {
    this.logger.debug(`Attempting to find cars by license: ${license}.`);
    try {
      const cars = Array.from(this.cars.values())
        .filter(car => car.license.equals(license))
        .sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
      this.logger.info(`Successfully found ${cars.length} cars for license: ${license}.`);
      return cars;
    } catch (error) {
      this.logger.error(`Error finding cars by license ${license}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async findByManufacturer(manufacturer: string): Promise<Car[]> {
    this.logger.debug(`Attempting to find cars by manufacturer: ${manufacturer}.`);
    try {
      const lowerManufacturer = manufacturer.toLowerCase();
      const cars = Array.from(this.cars.values())
        .filter(car => car.manufacturer.toString().toLowerCase() === lowerManufacturer)
        .sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
      this.logger.info(`Successfully found ${cars.length} cars for manufacturer: ${manufacturer}.`);
      return cars;
    } catch (error) {
      this.logger.error(`Error finding cars by manufacturer ${manufacturer}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

  async searchByName(query: string): Promise<Car[]> {
    this.logger.debug(`Attempting to search cars by name query: ${query}.`);
    try {
      const lowerQuery = query.toLowerCase();
      const cars = Array.from(this.cars.values())
        .filter(car =>
          car.name.toString().toLowerCase().includes(lowerQuery) ||
          car.shortName.toLowerCase().includes(lowerQuery) ||
          car.manufacturer.toString().toLowerCase().includes(lowerQuery)
        )
        .sort((a, b) => a.name.toString().localeCompare(b.name.toString()));
      this.logger.info(`Successfully found ${cars.length} cars matching search query: ${query}.`);
      return cars;
    } catch (error) {
      this.logger.error(`Error searching cars by name query ${query}:`, error instanceof Error ? error : new Error(String(error)));
      throw error;
    }
  }

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

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

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

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

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

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

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

  /**
   * Utility method to generate a new UUID
   */
  static generateId(): string {
    return uuidv4();
  }
}