const UnitOfWork = require('../../database/UnitOfWork');
const UserSettingsRepository = require('../../repositories/settings/UserSettingsRepository');
const { logger } = require('../../middleware/logger'); // <-- import logger

class UserSettingsController {
  static async getSettings(req, res) {
    const userId = req.user.userId;
    logger.info(`[UserSettingsController] getSettings called for userId: ${userId}`);
    const unitOfWork = new UnitOfWork();
    await unitOfWork.start();
    try {
      const repo = new UserSettingsRepository(unitOfWork);
      const settings = await repo.getSettingsByUserId(userId);
      await unitOfWork.commit();
      if (!settings) {
        logger.warn(`[UserSettingsController] No settings found for userId: ${userId}`);
        return res.status(404).json({ success: false, message: 'Settings not found' });
      }
      logger.info(`[UserSettingsController] getSettings success for userId: ${userId}`);
      res.json({ success: true, settings });
    } catch (error) {
      logger.error(`[UserSettingsController] getSettings error for userId: ${userId}`, { error });
      await unitOfWork.rollback(error);
      res.status(500).json({ success: false, message: error.message });
    }
  }
}

module.exports = UserSettingsController;
