package org.springblade.modules.martial.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.core.tool.utils.StringUtil;
import org.springblade.modules.martial.pojo.entity.MartialProject;
import org.springblade.modules.martial.service.IMartialProjectService;
import org.springframework.web.bind.annotation.*;

/**
 * 比赛项目 控制器
 *
 * @author BladeX
 */
@RestController
@AllArgsConstructor
@RequestMapping("/martial/project")
@Tag(name = "比赛项目管理", description = "比赛项目接口")
public class MartialProjectController extends BladeController {

	private final IMartialProjectService projectService;

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

	/**
	 * 分页列表
	 */
	@GetMapping("/list")
	@Operation(summary = "分页列表", description = "分页查询")
	public R<IPage<MartialProject>> list(MartialProject project, Query query) {
		QueryWrapper<MartialProject> queryWrapper = new QueryWrapper<>();
		// 赛事ID精确匹配
		if (project.getCompetitionId() != null) {
			queryWrapper.eq("competition_id", project.getCompetitionId());
		}
		// 项目名称模糊查询
		if (StringUtil.isNotBlank(project.getProjectName())) {
			queryWrapper.like("project_name", project.getProjectName());
		}
		// 分组类别模糊查询
		if (StringUtil.isNotBlank(project.getCategory())) {
			queryWrapper.like("category", project.getCategory());
		}
		// 项目类型精确匹配
		if (project.getEventType() != null) {
			queryWrapper.eq("event_type", project.getEventType());
		}
		// 参赛类型精确匹配
		if (project.getType() != null) {
			queryWrapper.eq("type", project.getType());
		}
		// 按排序字段和创建时间排序
		queryWrapper.orderByAsc("sort_order").orderByDesc("create_time");
		
		IPage<MartialProject> pages = projectService.page(Condition.getPage(query), queryWrapper);
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入实体")
	public R submit(@RequestBody MartialProject project) {
		// Auto-generate project code for new projects
		if (project.getId() == null && StringUtil.isBlank(project.getProjectCode())) {
			String projectCode = generateProjectCode(project.getCompetitionId());
			project.setProjectCode(projectCode);
		}
		return R.status(projectService.saveOrUpdate(project));
	}

	/**
	 * Generate project code: competition prefix + sequence number
	 * Format: C{competitionId}-P{sequence}, e.g., C1-P001
	 */
	private String generateProjectCode(Long competitionId) {
		if (competitionId == null) {
			return "P" + System.currentTimeMillis();
		}
		
		// Count existing projects for this competition
		LambdaQueryWrapper<MartialProject> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(MartialProject::getCompetitionId, competitionId);
		long count = projectService.count(wrapper);
		
		// Generate code: C{competitionId}-P{sequence}
		return String.format("C%d-P%03d", competitionId, count + 1);
	}

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

}
