/**
 * User Controller
 * 处理用户配置相关的 API 请求
 */

import { Request, Response } from 'express';
import { ConfigService } from '../services/config.service';

export class UserController {
  /**
   * 保存用户设置
   * POST /api/user/settings
   */
  static async saveSettings(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.user!.user_id;
      const { ocr, ai } = req.body;

      const config = await ConfigService.saveConfig(userId, { ocr, ai });

      res.status(200).json({
        success: true,
        data: config,
        message: '配置已保存',
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : '保存配置失败';
      res.status(400).json({
        success: false,
        error: message,
      });
    }
  }

  /**
   * 获取用户设置
   * GET /api/user/settings
   */
  static async getSettings(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.user!.user_id;

      const config = await ConfigService.getConfig(userId);

      res.status(200).json({
        success: true,
        data: config,
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : '获取配置失败';
      res.status(500).json({
        success: false,
        error: message,
      });
    }
  }

  /**
   * 清空用户设置
   * DELETE /api/user/settings
   */
  static async clearSettings(req: Request, res: Response): Promise<void> {
    try {
      const userId = req.user!.user_id;

      await ConfigService.clearConfig(userId);

      res.status(200).json({
        success: true,
        message: '配置已清空',
      });
    } catch (error) {
      const message = error instanceof Error ? error.message : '清空配置失败';
      res.status(500).json({
        success: false,
        error: message,
      });
    }
  }
}
