package org.springblade.modules.martial.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.martial.pojo.entity.MartialResult;
import org.springblade.modules.martial.service.IMartialResultService;
import org.springframework.web.bind.annotation.*;

/**
 * 成绩表 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/result")
@Tag(name = "成绩表管理", description = "成绩表接口")
public class MartialResultController extends BladeController {

	private final IMartialResultService resultService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入ID")
	public R<MartialResult> detail(@RequestParam Long id) {
		MartialResult detail = resultService.getById(id);
		return R.data(detail);
	}

	/**
	 * 分页列表
	 */
	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "分页查询")
	public R<IPage<MartialResult>> list(MartialResult result, Query query) {
		IPage<MartialResult> pages = resultService.page(Condition.getPage(query), Condition.getQueryWrapper(result));
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入实体")
	public R submit(@RequestBody MartialResult result) {
		return R.status(resultService.saveOrUpdate(result));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@Operation(summary = "删除", description = "传入ID")
	public R remove(@RequestParam String ids) {
		return R.status(resultService.removeByIds(Func.toLongList(ids)));
	}

	// ========== 成绩计算引擎 API ==========

	/**
	 * 计算运动员最终成绩
	 */
	@PostMapping("/calculate")
	@Operation(summary = "计算最终成绩", description = "根据裁判评分计算运动员最终成绩")
	public R<MartialResult> calculateScore(
		@RequestParam Long athleteId,
		@RequestParam Long projectId
	) {
		MartialResult result = resultService.calculateFinalScore(athleteId, projectId);
		return R.data(result);
	}

	/**
	 * 项目自动排名
	 */
	@PostMapping("/ranking")
	@Operation(summary = "自动排名", description = "根据最终成绩自动排名")
	public R autoRanking(@RequestParam Long projectId) {
		resultService.autoRanking(projectId);
		return R.success("排名完成");
	}

	/**
	 * 分配奖牌
	 */
	@PostMapping("/medals")
	@Operation(summary = "分配奖牌", description = "为前三名分配金银铜牌")
	public R assignMedals(@RequestParam Long projectId) {
		resultService.assignMedals(projectId);
		return R.success("奖牌分配完成");
	}

	/**
	 * 成绩复核
	 */
	@PostMapping("/review")
	@Operation(summary = "成绩复核", description = "复核并调整成绩")
	public R reviewResult(
		@RequestParam Long resultId,
		@RequestParam String reviewNote,
		@RequestParam(required = false) java.math.BigDecimal adjustment
	) {
		resultService.reviewResult(resultId, reviewNote, adjustment);
		return R.success("复核完成");
	}

	/**
	 * 发布成绩
	 */
	@PostMapping("/publish")
	@Operation(summary = "发布成绩", description = "将成绩标记为最终并发布")
	public R publishResults(@RequestParam Long projectId) {
		resultService.publishResults(projectId);
		return R.success("成绩已发布");
	}

	/**
	 * 撤销发布
	 */
	@PostMapping("/unpublish")
	@Operation(summary = "撤销发布", description = "撤销成绩发布状态")
	public R unpublishResults(@RequestParam Long projectId) {
		resultService.unpublishResults(projectId);
		return R.success("已撤销发布");
	}

}
