package com.zhentao.controller;

import com.zhentao.service.MatchmakerCheckinService;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 红娘签到控制器
 */
@RestController
@RequestMapping("/api/matchmaker/checkin")
public class MatchmakerCheckinController {

    @Autowired
    private MatchmakerCheckinService matchmakerCheckinService;

    /**
     * 执行签到
     *
     * @param makerId 用户ID（兼容旧参数名）
     * @param userId 用户ID
     * @return 签到结果
     */
    @PostMapping("/do")
    public Result<Boolean> checkin(@RequestParam(required = false) Long makerId, @RequestParam(required = false) Long userId) {
        try {
            // 优先使用 userId，如果没有则使用 makerId（兼容旧参数名）
            Long finalUserId = userId != null ? userId : makerId;
            System.out.println("📥 接收到签到请求: finalUserId=" + finalUserId);
            boolean result = matchmakerCheckinService.checkin(finalUserId);
            if (result) {
                Result<Boolean> successResult = new Result<>();
                successResult.setCode(200);
                successResult.setMsg("签到成功");
                successResult.setData(true);
                return successResult;
            } else {
                Result<Boolean> alreadyResult = new Result<>();
                alreadyResult.setCode(200);
                alreadyResult.setMsg("今日已签到");
                alreadyResult.setData(false);
                return alreadyResult;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Result<Boolean> errorResult = new Result<>();
            errorResult.setCode(500);
            errorResult.setMsg("签到失败：" + e.getMessage());
            errorResult.setData(false);
            return errorResult;
        }
    }

    /**
     * 检查是否已经签到
     *
     * @param makerId 用户ID（兼容旧参数名）
     * @param userId 用户ID
     * @return 签到状态
     */
    @GetMapping("/status")
    public Result<Boolean> isCheckedIn(@RequestParam(required = false) Long makerId, @RequestParam(required = false) Long userId) {
        try {
            // 优先使用 userId，如果没有则使用 makerId（兼容旧参数名）
            Long finalUserId = userId != null ? userId : makerId;
            boolean result = matchmakerCheckinService.isCheckedIn(finalUserId);
            Result<Boolean> statusResult = new Result<>();
            statusResult.setCode(200);
            statusResult.setMsg("查询成功");
            statusResult.setData(result);
            return statusResult;
        } catch (Exception e) {
            e.printStackTrace();
            Result<Boolean> errorResult = new Result<>();
            errorResult.setCode(500);
            errorResult.setMsg("检查签到状态失败：" + e.getMessage());
            errorResult.setData(false);
            return errorResult;
        }
    }

    /**
     * 获取签到统计信息
     *
     * @param makerId 用户ID（兼容旧参数名）
     * @param userId 用户ID
     * @return 签到统计
     */
    @GetMapping("/stats")
    public Result<Map<String, Integer>> getCheckinStats(@RequestParam(required = false) Long makerId, @RequestParam(required = false) Long userId) {
        try {
            // 优先使用 userId，如果没有则使用 makerId（兼容旧参数名）
            Long finalUserId = userId != null ? userId : makerId;
            Integer continuousDays = matchmakerCheckinService.getContinuousDays(finalUserId);
            Integer totalDays = matchmakerCheckinService.getTotalDays(finalUserId);

            Map<String, Integer> stats = new HashMap<>();
            stats.put("continuousDays", continuousDays);
            stats.put("totalDays", totalDays);

            Result<Map<String, Integer>> statsResult = new Result<>();
            statsResult.setCode(200);
            statsResult.setMsg("查询成功");
            statsResult.setData(stats);
            return statsResult;
        } catch (Exception e) {
            e.printStackTrace();
            Result<Map<String, Integer>> errorResult = new Result<>();
            errorResult.setCode(500);
            errorResult.setMsg("获取签到统计失败：" + e.getMessage());
            errorResult.setData(null);
            return errorResult;
        }
    }
}