package org.springblade.modules.martial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.martial.pojo.entity.MartialJudgeProject;
import org.springblade.modules.martial.mapper.MartialJudgeProjectMapper;
import org.springblade.modules.martial.service.IMartialJudgeProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 裁判项目关联 服务实现类
 *
 * @author BladeX
 */
@Slf4j
@Service
public class MartialJudgeProjectServiceImpl extends ServiceImpl<MartialJudgeProjectMapper, MartialJudgeProject>
	implements IMartialJudgeProjectService {

	/**
	 * Task 2.5: 检查裁判是否有权限给项目打分
	 *
	 * @param judgeId 裁判ID
	 * @param projectId 项目ID
	 * @return 是否有权限
	 */
	@Override
	public boolean hasPermission(Long judgeId, Long projectId) {
		if (judgeId == null || projectId == null) {
			return false;
		}

		// 查询裁判-项目关联记录
		Long count = this.lambdaQuery()
			.eq(MartialJudgeProject::getJudgeId, judgeId)
			.eq(MartialJudgeProject::getProjectId, projectId)
			.eq(MartialJudgeProject::getStatus, 1)
			.eq(MartialJudgeProject::getIsDeleted, 0)
			.count();

		return count > 0;
	}

	/**
	 * Task 2.5: 批量分配裁判到项目
	 *
	 * @param competitionId 赛事ID
	 * @param projectId 项目ID
	 * @param judgeIds 裁判ID列表
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void assignJudgesToProject(Long competitionId, Long projectId, List<Long> judgeIds) {
		if (judgeIds == null || judgeIds.isEmpty()) {
			return;
		}

		// 先删除项目的旧分配（逻辑删除）
		this.lambdaUpdate()
			.eq(MartialJudgeProject::getCompetitionId, competitionId)
			.eq(MartialJudgeProject::getProjectId, projectId)
			.set(MartialJudgeProject::getIsDeleted, 1)
			.update();

		// 批量插入新分配
		List<MartialJudgeProject> assignments = new ArrayList<>();
		for (Long judgeId : judgeIds) {
			MartialJudgeProject assignment = new MartialJudgeProject();
			assignment.setCompetitionId(competitionId);
			assignment.setJudgeId(judgeId);
			assignment.setProjectId(projectId);
			assignment.setAssignTime(LocalDateTime.now());
			assignment.setStatus(1);
			assignments.add(assignment);
		}

		this.saveBatch(assignments);

		log.info("✅ 裁判分配完成 - 赛事ID:{}, 项目ID:{}, 分配裁判数:{}",
			competitionId, projectId, judgeIds.size());
	}

	/**
	 * Task 2.5: 获取裁判负责的所有项目
	 *
	 * @param judgeId 裁判ID
	 * @param competitionId 赛事ID
	 * @return 项目ID列表
	 */
	@Override
	public List<Long> getJudgeProjects(Long judgeId, Long competitionId) {
		return this.lambdaQuery()
			.eq(MartialJudgeProject::getJudgeId, judgeId)
			.eq(MartialJudgeProject::getCompetitionId, competitionId)
			.eq(MartialJudgeProject::getStatus, 1)
			.eq(MartialJudgeProject::getIsDeleted, 0)
			.list()
			.stream()
			.map(MartialJudgeProject::getProjectId)
			.collect(Collectors.toList());
	}

	/**
	 * Task 2.5: 获取项目的所有裁判
	 *
	 * @param projectId 项目ID
	 * @return 裁判ID列表
	 */
	@Override
	public List<Long> getProjectJudges(Long projectId) {
		return this.lambdaQuery()
			.eq(MartialJudgeProject::getProjectId, projectId)
			.eq(MartialJudgeProject::getStatus, 1)
			.eq(MartialJudgeProject::getIsDeleted, 0)
			.list()
			.stream()
			.map(MartialJudgeProject::getJudgeId)
			.collect(Collectors.toList());
	}

}
