// src/controllers/model.controller.js
// 모델 컨트롤러

const { LLMProvider } = require('../models');

/**
 * 사용 가능한 모델 목록 조회
 */
exports.list = async (req, res, next) => {
  try {
    const providers = await LLMProvider.getActiveProviders();

    // OpenAI 호환 형식으로 변환
    const models = providers.map((provider) => ({
      id: provider.modelName,
      object: 'model',
      created: Math.floor(new Date(provider.createdAt).getTime() / 1000),
      owned_by: provider.name,
      permission: [],
      root: provider.modelName,
      parent: null,
    }));

    return res.json({
      object: 'list',
      data: models,
    });
  } catch (error) {
    return next(error);
  }
};

/**
 * 모델 상세 정보 조회
 */
exports.get = async (req, res, next) => {
  try {
    const { id } = req.params;

    const provider = await LLMProvider.findOne({
      where: { modelName: id, isActive: true },
    });

    if (!provider) {
      return res.status(404).json({
        error: {
          message: `모델 '${id}'을(를) 찾을 수 없습니다.`,
          type: 'invalid_request_error',
          param: 'model',
          code: 'model_not_found',
        },
      });
    }

    return res.json({
      id: provider.modelName,
      object: 'model',
      created: Math.floor(new Date(provider.createdAt).getTime() / 1000),
      owned_by: provider.name,
      permission: [],
      root: provider.modelName,
      parent: null,
    });
  } catch (error) {
    return next(error);
  }
};
