package com.lesingle.creation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.registration.*;
import com.lesingle.creation.service.ContestRegistrationService;
import com.lesingle.creation.vo.registration.RegistrationStatsVO;
import com.lesingle.creation.vo.registration.RegistrationVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 竞赛报名管理控制器
 */
@Tag(name = "竞赛报名管理")
@RestController
@RequestMapping("/api/contests/registrations")
@RequiredArgsConstructor
public class ContestRegistrationController {

    private final ContestRegistrationService registrationService;

    @PostMapping
    @Operation(summary = "创建报名")
    @PreAuthorize("hasAuthority('contest:register')")
    public Result<RegistrationVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateRegistrationDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long creatorId = userPrincipal.getUserId();
        RegistrationVO result = registrationService.create(dto, tenantId, creatorId);
        return Result.success(result);
    }

    @GetMapping("/stats")
    @Operation(summary = "报名统计")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<RegistrationStatsVO> getStats(@RequestParam(required = false) Long contestId) {
        RegistrationStatsVO result = registrationService.getStats(contestId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "分页查询报名列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<Page<RegistrationVO>> pageQuery(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @ModelAttribute RegistrationQueryDTO queryDTO) {
        Long tenantId = userPrincipal.getTenantId();
        Page<RegistrationVO> result = registrationService.pageQuery(queryDTO, tenantId);
        return Result.success(result);
    }

    @GetMapping("/my/{contestId}")
    @Operation(summary = "获取用户在某活动中的报名记录")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<RegistrationVO> getMyRegistration(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long contestId) {
        Long userId = userPrincipal.getUserId();
        RegistrationVO result = registrationService.getMyRegistration(contestId, userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取报名详情")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<RegistrationVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        RegistrationVO result = registrationService.getDetail(id, tenantId);
        return Result.success(result);
    }

    @PatchMapping("/{id}/review")
    @Operation(summary = "审核报名")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<RegistrationVO> review(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated ReviewRegistrationDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long modifierId = userPrincipal.getUserId();
        RegistrationVO result = registrationService.review(id, dto, tenantId, modifierId);
        return Result.success(result);
    }

    @PostMapping("/{id}/teachers")
    @Operation(summary = "添加指导老师")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<RegistrationVO> addTeacher(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated AddTeacherDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long operatorId = userPrincipal.getUserId();
        RegistrationVO result = registrationService.addTeacher(id, dto.getTeacherUserId(), tenantId, operatorId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}/teachers/{teacherUserId}")
    @Operation(summary = "移除指导老师")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<Void> removeTeacher(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @PathVariable Long teacherUserId) {
        Long tenantId = userPrincipal.getTenantId();
        registrationService.removeTeacher(id, teacherUserId, tenantId);
        return Result.success(null);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除报名")
    @PreAuthorize("hasAuthority('contest:update')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        registrationService.delete(id, tenantId);
        return Result.success(null);
    }
}
