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.MartialExceptionEvent;
import org.springblade.modules.martial.service.IMartialExceptionEventService;
import org.springframework.web.bind.annotation.*;

/**
 * 异常事件 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/exception")
@Tag(name = "异常事件管理", description = "比赛日异常事件处理接口")
public class MartialExceptionEventController extends BladeController {

	private final IMartialExceptionEventService exceptionEventService;

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

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

	/**
	 * Task 2.4: 记录异常事件
	 */
	@PostMapping("/record")
	@Operation(summary = "记录异常事件", description = "比赛日异常情况记录")
	public R recordException(
		@RequestParam Long competitionId,
		@RequestParam(required = false) Long scheduleId,
		@RequestParam(required = false) Long athleteId,
		@RequestParam Integer eventType,
		@RequestParam String eventDescription
	) {
		exceptionEventService.recordException(competitionId, scheduleId, athleteId, eventType, eventDescription);
		return R.success("异常事件已记录");
	}

	/**
	 * Task 2.4: 处理异常事件
	 */
	@PostMapping("/handle")
	@Operation(summary = "处理异常事件", description = "标记异常事件为已处理")
	public R handleException(
		@RequestParam Long eventId,
		@RequestParam String handlerName,
		@RequestParam String handleResult
	) {
		exceptionEventService.handleException(eventId, handlerName, handleResult);
		return R.success("异常事件已处理");
	}

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

}
