import { getAllKeyValStore } from '@/src/dbService'
// import redisClient from '@/src/lib/redis-client'
import { CONST_KEYS, CONST_KEYS_ARRAY, type ConstKey } from '@/src/lib/const-keys'

// const CONST_REDIS_PREFIX = 'const:';

export const computeConstants = async (): Promise<void> => {
  try {
    console.log('Computing constants from database...');

    /*
    // Old implementation - direct DB queries:
    import { db } from '@/src/db/db_index'
    import { keyValStore } from '@/src/db/schema'

    const constants = await db.select().from(keyValStore);
    */

    const constants = await getAllKeyValStore();

    // for (const constant of constants) {
    //   const redisKey = `${CONST_REDIS_PREFIX}${constant.key}`;
    //   const value = JSON.stringify(constant.value);
    //   await redisClient.set(redisKey, value);
    // }

    console.log(`Computed ${constants.length} constants from DB`);
  } catch (error) {
    console.error('Failed to compute constants:', error);
    throw error;
  }
};

export const getConstant = async <T = any>(key: string): Promise<T | null> => {
  // const redisKey = `${CONST_REDIS_PREFIX}${key}`;
  // const value = await redisClient.get(redisKey);
  //
  // if (!value) {
  //   return null;
  // }
  //
  // try {
  //   return JSON.parse(value) as T;
  // } catch {
  //   return value as unknown as T;
  // }

  const constants = await getAllKeyValStore();
  const entry = constants.find(c => c.key === key);

  if (!entry) {
    return null;
  }

  return entry.value as T;
};

export const getConstants = async <T = any>(keys: string[]): Promise<Record<string, T | null>> => {
  // const redisKeys = keys.map(key => `${CONST_REDIS_PREFIX}${key}`);
  // const values = await redisClient.MGET(redisKeys);
  //
  // const result: Record<string, T | null> = {};
  // keys.forEach((key, index) => {
  //   const value = values[index];
  //   if (!value) {
  //     result[key] = null;
  //   } else {
  //     try {
  //       result[key] = JSON.parse(value) as T;
  //     } catch {
  //       result[key] = value as unknown as T;
  //     }
  //   }
  // });
  //
  // return result;

  const constants = await getAllKeyValStore();
  const constantsMap = new Map(constants.map(c => [c.key, c.value]));

  const result: Record<string, T | null> = {};
  for (const key of keys) {
    const value = constantsMap.get(key);
    result[key] = (value !== undefined ? value : null) as T | null;
  }

  return result;
};

export const getAllConstValues = async (): Promise<Record<ConstKey, any>> => {
  // const result: Record<string, any> = {};
  //
  // for (const key of CONST_KEYS_ARRAY) {
  //   result[key] = await getConstant(key);
  // }
  //
  // return result as Record<ConstKey, any>;

  const constants = await getAllKeyValStore();
  const constantsMap = new Map(constants.map(c => [c.key, c.value]));

  const result: Record<string, any> = {};
  for (const key of CONST_KEYS_ARRAY) {
    result[key] = constantsMap.get(key) ?? null;
  }

  return result as Record<ConstKey, any>;
};

export { CONST_KEYS, CONST_KEYS_ARRAY };
