package com.yimeng.okr.controller;

import com.yimeng.okr.annotation.OperationLogAnnotation;
import com.yimeng.okr.dto.ApiResult;
import com.yimeng.okr.entity.ScoreAppeal;
import com.yimeng.okr.security.SecurityUtils;
import com.yimeng.okr.service.ScoreAppealService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/api/score-appeals")
@RequiredArgsConstructor
public class ScoreAppealController {

    private final ScoreAppealService appealService;

    @GetMapping
    @OperationLogAnnotation(action = "VIEW_SCORE_APPEALS", entityType = "score_appeal", sensitive = true)
    public ApiResult<List<ScoreAppeal>> listAppeals(@RequestParam(required = false) String status) {
        return ApiResult.success(appealService.listAppeals(SecurityUtils.getCurrentUserId(), status));
    }

    @PostMapping
    @OperationLogAnnotation(action = "SUBMIT_SCORE_APPEAL", entityType = "score_appeal", sensitive = true)
    public ApiResult<ScoreAppeal> submitAppeal(@RequestBody Map<String, Object> body) {
        Long periodId = Long.valueOf(body.get("periodId").toString());
        String reason = body.getOrDefault("reason", "").toString();
        return ApiResult.success(appealService.submitAppeal(periodId, SecurityUtils.getCurrentUserId(), reason));
    }

    @PutMapping("/{id}/resolve")
    @OperationLogAnnotation(action = "RESOLVE_SCORE_APPEAL", entityType = "score_appeal", sensitive = true)
    public ApiResult<ScoreAppeal> resolveAppeal(@PathVariable Long id, @RequestBody Map<String, Object> body) {
        String status = body.getOrDefault("status", "").toString();
        String reply = body.getOrDefault("reply", "").toString();
        return ApiResult.success(appealService.resolveAppeal(id, SecurityUtils.getCurrentUserId(), status, reply));
    }
}
