/**
 * @ai-summary Platform-specific Redis caching service
 * @ai-context Caching layer for vehicle data and VIN decoding
 */
import { CacheService } from '../../../core/config/redis';
import { logger } from '../../../core/logging/logger';

export class PlatformCacheService {
  private cacheService: CacheService;
  private readonly prefix = 'platform:';

  constructor(cacheService: CacheService) {
    this.cacheService = cacheService;
  }

  /**
   * Get cached years
   */
  async getYears(): Promise<number[] | null> {
    const key = this.prefix + 'years';
    return await this.cacheService.get<number[]>(key);
  }

  /**
   * Set cached years
   */
  async setYears(years: number[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'years';
    await this.cacheService.set(key, years, ttl);
  }

  /**
   * Get cached makes for year
   */
  async getMakes(year: number): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:makes:' + year;
    return await this.cacheService.get<string[]>(key);
  }

  /**
   * Set cached makes for year
   */
  async setMakes(year: number, makes: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:makes:' + year;
    await this.cacheService.set(key, makes, ttl);
  }

  /**
   * Get cached models for year and make
   */
  async getModels(year: number, make: string): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:models:' + year + ':' + make;
    return await this.cacheService.get<string[]>(key);
  }

  /**
   * Set cached models for year and make
   */
  async setModels(year: number, make: string, models: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:models:' + year + ':' + make;
    await this.cacheService.set(key, models, ttl);
  }

  /**
   * Get cached trims for year, make, and model
   */
  async getTrims(year: number, make: string, model: string): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:trims:' + year + ':' + make + ':' + model;
    return await this.cacheService.get<string[]>(key);
  }

  /**
   * Set cached trims for year, make, and model
   */
  async setTrims(year: number, make: string, model: string, trims: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:trims:' + year + ':' + make + ':' + model;
    await this.cacheService.set(key, trims, ttl);
  }

  /**
   * Get cached engines for year, make, model, and trim
   */
  async getEngines(year: number, make: string, model: string, trim: string): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:engines:' + year + ':' + make + ':' + model + ':' + trim;
    return await this.cacheService.get<string[]>(key);
  }

  /**
   * Set cached engines for year, make, model, and trim
   */
  async setEngines(year: number, make: string, model: string, trim: string, engines: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:engines:' + year + ':' + make + ':' + model + ':' + trim;
    await this.cacheService.set(key, engines, ttl);
  }

  /**
   * Get cached transmissions for year, make, model, and trim
   */
  async getTransmissionsForTrim(year: number, make: string, model: string, trim: string): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:transmissions:' + year + ':' + make + ':' + model + ':' + trim;
    return await this.cacheService.get<string[]>(key);
  }

  /**
   * Set cached transmissions for year, make, model, and trim
   */
  async setTransmissionsForTrim(year: number, make: string, model: string, trim: string, transmissions: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:transmissions:' + year + ':' + make + ':' + model + ':' + trim;
    await this.cacheService.set(key, transmissions, ttl);
  }

  async getTransmissionsForTrimAndEngine(year: number, make: string, model: string, trim: string, engine: string): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:transmissions:' + year + ':' + make + ':' + model + ':' + trim + ':engine:' + engine;
    return await this.cacheService.get<string[]>(key);
  }

  async setTransmissionsForTrimAndEngine(year: number, make: string, model: string, trim: string, engine: string, transmissions: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:transmissions:' + year + ':' + make + ':' + model + ':' + trim + ':engine:' + engine;
    await this.cacheService.set(key, transmissions, ttl);
  }

  async getEnginesForTrimAndTransmission(year: number, make: string, model: string, trim: string, transmission: string): Promise<string[] | null> {
    const key = this.prefix + 'vehicle-data:engines:' + year + ':' + make + ':' + model + ':' + trim + ':transmission:' + transmission;
    return await this.cacheService.get<string[]>(key);
  }

  async setEnginesForTrimAndTransmission(year: number, make: string, model: string, trim: string, transmission: string, engines: string[], ttl: number = 6 * 3600): Promise<void> {
    const key = this.prefix + 'vehicle-data:engines:' + year + ':' + make + ':' + model + ':' + trim + ':transmission:' + transmission;
    await this.cacheService.set(key, engines, ttl);
  }

  /**
   * Get cached VIN decode result
   */
  async getVINDecode(vin: string): Promise<any | null> {
    const key = this.prefix + 'vin-decode:' + vin.toUpperCase();
    return await this.cacheService.get<any>(key);
  }

  /**
   * Set cached VIN decode result (7 days for successful decodes, 1 hour for failures)
   */
  async setVINDecode(vin: string, result: any, success: boolean = true): Promise<void> {
    const key = this.prefix + 'vin-decode:' + vin.toUpperCase();
    const ttl = success ? 7 * 24 * 3600 : 3600;
    await this.cacheService.set(key, result, ttl);
  }

  /**
   * Invalidate all vehicle data cache (for admin operations)
   */
  async invalidateVehicleData(): Promise<void> {
    try {
      const yearsKey = this.prefix + 'years';
      await Promise.all([
        this.cacheService.del(yearsKey),
        this.cacheService.deletePattern(this.prefix + 'vehicle-data:*')
      ]);
      logger.debug('Vehicle data cache invalidated');
    } catch (error) {
      logger.error('Vehicle data cache invalidation failed', { error });
    }
  }
}
