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.dto.AthleteOrderDTO;
import org.springblade.modules.martial.pojo.dto.MoveAthletesDTO;
import org.springblade.modules.martial.pojo.entity.MartialScheduleConflict;
import org.springblade.modules.martial.pojo.entity.MartialSchedulePlan;
import org.springblade.modules.martial.service.IMartialSchedulePlanService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编排方案控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/schedule-plan")
@Tag(name = "编排调度管理", description = "编排调度相关接口")
public class MartialSchedulePlanController extends BladeController {

	private final IMartialSchedulePlanService schedulePlanService;

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

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

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

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

	// ========== 编排调度核心功能 API ==========

	/**
	 * 自动编排
	 */
	@PostMapping("/auto-schedule")
	@Operation(summary = "自动编排", description = "根据赛事ID自动生成编排方案")
	public R<MartialSchedulePlan> autoSchedule(@RequestParam Long competitionId) {
		MartialSchedulePlan plan = schedulePlanService.autoSchedule(competitionId);
		return R.data(plan);
	}

	/**
	 * 冲突检测
	 */
	@GetMapping("/detect-conflicts")
	@Operation(summary = "冲突检测", description = "检测编排方案中的冲突")
	public R<List<MartialScheduleConflict>> detectConflicts(@RequestParam Long planId) {
		List<MartialScheduleConflict> conflicts = schedulePlanService.detectConflicts(planId);
		return R.data(conflicts);
	}

	/**
	 * 检测移动冲突
	 */
	@PostMapping("/check-move-conflicts")
	@Operation(summary = "检测移动冲突", description = "检测移动运动员是否会产生冲突")
	public R<List<MartialScheduleConflict>> checkMoveConflicts(@RequestBody MoveAthletesDTO moveDTO) {
		List<MartialScheduleConflict> conflicts = schedulePlanService.checkMoveConflicts(moveDTO);
		return R.data(conflicts);
	}

	/**
	 * 移动运动员
	 */
	@PostMapping("/move-athletes")
	@Operation(summary = "移动运动员", description = "批量移动运动员到其他时间槽")
	public R<Boolean> moveAthletes(@RequestBody MoveAthletesDTO moveDTO) {
		Boolean result = schedulePlanService.moveAthletes(moveDTO);
		return R.data(result);
	}

	/**
	 * 调整出场顺序
	 */
	@PostMapping("/update-order")
	@Operation(summary = "调整出场顺序", description = "调整场地内运动员出场顺序")
	public R<Boolean> updateAppearanceOrder(
		@RequestParam Long slotId,
		@RequestBody List<AthleteOrderDTO> newOrder
	) {
		Boolean result = schedulePlanService.updateAppearanceOrder(slotId, newOrder);
		return R.data(result);
	}

	/**
	 * 确认并发布方案
	 */
	@PostMapping("/confirm-and-publish")
	@Operation(summary = "确认并发布", description = "确认编排方案并发布")
	public R<Boolean> confirmAndPublish(@RequestParam Long planId) {
		Boolean result = schedulePlanService.confirmAndPublishPlan(planId);
		return R.data(result);
	}

	/**
	 * 解决冲突
	 */
	@PostMapping("/resolve-conflicts")
	@Operation(summary = "解决冲突", description = "标记冲突为已解决")
	public R<Boolean> resolveConflicts(
		@RequestParam Long planId,
		@RequestBody List<MartialScheduleConflict> conflicts
	) {
		Boolean result = schedulePlanService.resolveConflicts(planId, conflicts);
		return R.data(result);
	}

}
