import fs from 'fs';
import path from 'path';
import { getLightconeInfoApi } from '../api';
import { LightConeDetail } from '@/types';

const DATA_DIR = path.join(process.cwd(), 'data');
const lightconeFileCache: Record<string, Record<string, LightConeDetail>> = {};
export let lightconeMap: Record<string, LightConeDetail> = {};
export let lightconeBonusMap: Record<string, Record<string, { type: string, value: number }[]>> = {};

function getJsonFilePath(locale: string): string {
  return path.join(DATA_DIR, `lightcones.${locale}.json`);
}

function loadLightconeFromFileIfExists(locale: string): Record<string, LightConeDetail> | null {
  if (lightconeFileCache[locale]) return lightconeFileCache[locale];

  const filePath = getJsonFilePath(locale);
  const fileBonusPath = path.join(DATA_DIR, `lightcone_bonus.json`);
  if (fs.existsSync(fileBonusPath)) {
    const data = JSON.parse(fs.readFileSync(fileBonusPath, 'utf-8')) as Record<string, Record<string, { type: string, value: number }[]>>;
    lightconeBonusMap = data;
  }
  if (fs.existsSync(filePath)) {
    const data = JSON.parse(fs.readFileSync(filePath, 'utf-8')) as Record<string, LightConeDetail>;
    Object.keys(data).forEach((key) => {
      data[key].Bonus = lightconeBonusMap[key] || {};
    });
    lightconeFileCache[locale] = data;

    return data;
  }
  return null;
}

export async function loadLightcones(charIds: string[], locale: string): Promise<Record<string, LightConeDetail>> {
  const fileData = loadLightconeFromFileIfExists(locale);
  const fileIds = fileData ? Object.keys(fileData) : [];

  if (fileData && charIds.every(id => fileIds.includes(id))) {
    lightconeMap = fileData;
    return lightconeMap;
  }

  const result: Record<string, LightConeDetail> = {};

  await Promise.all(
    charIds.map(async id => {
      const info = await getLightconeInfoApi(Number(id), locale);
      if (info) {
        info.Bonus = lightconeBonusMap[id] || {};
        result[id] = info;
      }
    })
  );

  fs.mkdirSync(DATA_DIR, { recursive: true });
  const filePath = getJsonFilePath(locale);
  fs.writeFileSync(filePath, JSON.stringify(result, null, 2), 'utf-8');

  lightconeFileCache[locale] = result;
  lightconeMap = result;
  return result;
}
