package top.tqx.demo_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.tqx.demo_1.common.Result;
import top.tqx.demo_1.service.impl.WrongQuestionSyncValidator;

import java.time.LocalDateTime;

@Tag(name = "数据同步验证", description = "验证错题与批阅系统之间的数据同步机制")
@RestController
@RequestMapping("/data-sync")
@Slf4j
public class DataSyncController extends BaseController {

    @Autowired
    private WrongQuestionSyncValidator syncValidator;

    @Operation(summary = "验证指定用户的数据同步", description = "验证指定用户的错题数据与批阅数据是否一致")
    @GetMapping("/validate/user/{userId}")
    public Result<WrongQuestionSyncValidator.ValidationResult> validateUserSync(
            @PathVariable Long userId,
            @RequestParam(required = false) String paperId,
            @RequestParam(required = false) String sessionId) {

        log.info("验证用户数据同步: userId={}, paperId={}, sessionId={}", userId, paperId, sessionId);

        try {
            WrongQuestionSyncValidator.ValidationResult result = 
                    syncValidator.validateSyncIntegrity(userId, paperId, sessionId);
            
            if (result.isSuccess()) {
                return Result.success("数据同步验证通过", result);
            } else {
                return Result.error(400, "数据同步验证失败", result);
            }
        } catch (Exception e) {
            log.error("验证用户数据同步异常: {}", e.getMessage(), e);
            return Result.error(500, "验证异常: " + e.getMessage());
        }
    }

    @Operation(summary = "验证当前用户的数据同步", description = "验证当前登录用户的错题数据与批阅数据是否一致")
    @GetMapping("/validate/me")
    public Result<WrongQuestionSyncValidator.ValidationResult> validateCurrentUserSync(
            HttpServletRequest httpRequest,
            @RequestParam(required = false) String paperId,
            @RequestParam(required = false) String sessionId) {

        Long userId = getCurrentUserId(httpRequest);
        if (userId == null) {
            return Result.error(401, "用户未登录");
        }

        log.info("验证当前用户数据同步: userId={}, paperId={}, sessionId={}", userId, paperId, sessionId);

        try {
            WrongQuestionSyncValidator.ValidationResult result = 
                    syncValidator.validateSyncIntegrity(userId, paperId, sessionId);
            
            if (result.isSuccess()) {
                return Result.success("数据同步验证通过", result);
            } else {
                return Result.error(400, "数据同步验证失败", result);
            }
        } catch (Exception e) {
            log.error("验证当前用户数据同步异常: {}", e.getMessage(), e);
            return Result.error(500, "验证异常: " + e.getMessage());
        }
    }

    @Operation(summary = "全量验证所有用户数据同步", description = "验证系统中所有用户的错题数据与批阅数据是否一致")
    @GetMapping("/validate/all")
    public Result<WrongQuestionSyncValidator.ValidationResult> validateAllUsersSync(
            @RequestParam(defaultValue = "100") int pageSize) {

        log.info("全量验证数据同步: pageSize={}", pageSize);

        try {
            WrongQuestionSyncValidator.ValidationResult result = 
                    syncValidator.validateAllUsers(pageSize);
            
            if (result.isSuccess()) {
                return Result.success("全量数据同步验证通过", result);
            } else {
                return Result.error(400, "全量数据同步验证失败", result);
            }
        } catch (Exception e) {
            log.error("全量验证数据同步异常: {}", e.getMessage(), e);
            return Result.error(500, "验证异常: " + e.getMessage());
        }
    }

    @Operation(summary = "验证最近答题记录", description = "验证指定时间之后的答题记录是否完整")
    @GetMapping("/validate/recent")
    public Result<WrongQuestionSyncValidator.ValidationResult> validateRecentAnswers(
            @RequestParam(defaultValue = "24") int hoursAgo) {

        LocalDateTime startTime = LocalDateTime.now().minusHours(hoursAgo);
        log.info("验证最近答题记录: startTime={}", startTime);

        try {
            WrongQuestionSyncValidator.ValidationResult result = 
                    syncValidator.validateRecentAnswers(startTime);
            
            if (result.isSuccess()) {
                return Result.success("最近答题记录验证通过", result);
            } else {
                return Result.error(400, "最近答题记录验证失败", result);
            }
        } catch (Exception e) {
            log.error("验证最近答题记录异常: {}", e.getMessage(), e);
            return Result.error(500, "验证异常: " + e.getMessage());
        }
    }
}
