package com.competition.modules.biz.contest.controller;

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.biz.contest.dto.CreateRegistrationDto;
import com.competition.modules.biz.contest.dto.QueryRegistrationDto;
import com.competition.modules.biz.contest.service.IContestRegistrationService;
import com.competition.security.annotation.RequirePermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "报名管理")
@RestController
@RequestMapping("/contests/registrations")
@RequiredArgsConstructor
public class ContestRegistrationController {

    private final IContestRegistrationService registrationService;

    @PostMapping
    @RequirePermission("contest:register")
    @Operation(summary = "创建报名")
    public Result<Map<String, Object>> create(@Valid @RequestBody CreateRegistrationDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(registrationService.createRegistration(dto, tenantId, userId));
    }

    @GetMapping("/stats")
    @RequirePermission("contest:read")
    @Operation(summary = "获取报名统计")
    public Result<Map<String, Object>> getStats(@RequestParam(required = false) Long contestId) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperAdmin = SecurityUtil.isSuperAdmin();
        return Result.success(registrationService.getStats(contestId, tenantId, isSuperAdmin));
    }

    @GetMapping
    @RequirePermission("contest:read")
    @Operation(summary = "查询报名列表")
    public Result<PageResult<Map<String, Object>>> findAll(QueryRegistrationDto dto) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        boolean isSuperTenant = SecurityUtil.isSuperAdmin();
        return Result.success(registrationService.findAll(dto, tenantId, isSuperTenant));
    }

    @GetMapping("/my/{contestId}")
    @RequirePermission("contest:read")
    @Operation(summary = "获取我的报名信息")
    public Result<Map<String, Object>> getMyRegistration(@PathVariable Long contestId) {
        Long userId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(registrationService.getMyRegistration(contestId, userId, tenantId));
    }

    @GetMapping("/{id}")
    @RequirePermission("contest:read")
    @Operation(summary = "查询报名详情")
    public Result<Map<String, Object>> findDetail(@PathVariable Long id) {
        return Result.success(registrationService.findDetail(id, SecurityUtil.getCurrentTenantId()));
    }

    @PatchMapping("/{id}/review")
    @RequirePermission("contest:update")
    @Operation(summary = "审核报名")
    public Result<Void> review(@PathVariable Long id, @RequestBody Map<String, String> body) {
        Long operatorId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        registrationService.reviewRegistration(id, body.get("registrationState"), body.get("reason"), operatorId, tenantId);
        return Result.success();
    }

    @PatchMapping("/{id}/revoke")
    @RequirePermission("contest:update")
    @Operation(summary = "撤回审核")
    public Result<Void> revoke(@PathVariable Long id) {
        registrationService.revokeReview(id, SecurityUtil.getCurrentTenantId());
        return Result.success();
    }

    @PostMapping("/batch-review")
    @RequirePermission("contest:update")
    @Operation(summary = "批量审核报名")
    @SuppressWarnings("unchecked")
    public Result<Void> batchReview(@RequestBody Map<String, Object> body) {
        List<Long> ids = (List<Long>) body.get("ids");
        String registrationState = (String) body.get("registrationState");
        String reason = (String) body.get("reason");
        Long operatorId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        registrationService.batchReview(ids, registrationState, reason, operatorId, tenantId);
        return Result.success();
    }

    @PostMapping("/{id}/teachers")
    @RequirePermission("contest:update")
    @Operation(summary = "添加指导老师")
    public Result<Void> addTeacher(@PathVariable Long id, @RequestBody Map<String, Long> body) {
        Long tenantId = SecurityUtil.getCurrentTenantId();
        Long creatorId = SecurityUtil.getCurrentUserId();
        registrationService.addTeacher(id, body.get("teacherUserId"), tenantId, creatorId);
        return Result.success();
    }

    @DeleteMapping("/{id}/teachers/{teacherUserId}")
    @RequirePermission("contest:update")
    @Operation(summary = "移除指导老师")
    public Result<Void> removeTeacher(@PathVariable Long id, @PathVariable Long teacherUserId) {
        registrationService.removeTeacher(id, teacherUserId, SecurityUtil.getCurrentTenantId());
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @RequirePermission("contest:update")
    @Operation(summary = "删除报名")
    public Result<Void> remove(@PathVariable Long id) {
        registrationService.removeRegistration(id, SecurityUtil.getCurrentTenantId());
        return Result.success();
    }
}
