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.work.SubmitWorkDTO;
import com.lesingle.creation.dto.work.WorkQueryDTO;
import com.lesingle.creation.service.ContestWorkService;
import com.lesingle.creation.vo.work.WorkStatsVO;
import com.lesingle.creation.vo.work.WorkVO;
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.*;

import java.util.List;

/**
 * 竞赛作品管理控制器
 */
@Tag(name = "竞赛作品管理")
@RestController
@RequestMapping("/api/contests/works")
@RequiredArgsConstructor
public class ContestWorkController {

    private final ContestWorkService workService;

    @PostMapping
    @Operation(summary = "提交作品")
    @PreAuthorize("hasAuthority('contest:work:submit')")
    public Result<WorkVO> submit(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated SubmitWorkDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long submitterUserId = userPrincipal.getUserId();
        WorkVO result = workService.submit(dto, tenantId, submitterUserId);
        return Result.success(result);
    }

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

    @GetMapping("/page")
    @Operation(summary = "分页查询作品列表")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<Page<WorkVO>> pageQuery(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @ModelAttribute WorkQueryDTO queryDTO) {
        Long tenantId = userPrincipal.getTenantId();
        Page<WorkVO> result = workService.pageQuery(queryDTO, tenantId);
        return Result.success(result);
    }

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

    @PutMapping("/{id}/return")
    @Operation(summary = "退回作品")
    @PreAuthorize("hasAuthority('contest:work:update')")
    public Result<WorkVO> returnWork(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestParam String reason) {
        Long tenantId = userPrincipal.getTenantId();
        WorkVO result = workService.returnWork(id, tenantId, reason);
        return Result.success(result);
    }

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

    @GetMapping("/registration/{registrationId}")
    @Operation(summary = "查询某报名的所有作品")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<WorkVO>> listByRegistration(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long registrationId) {
        Long tenantId = userPrincipal.getTenantId();
        List<WorkVO> result = workService.listByRegistration(registrationId, tenantId);
        return Result.success(result);
    }

    @GetMapping("/registration/{registrationId}/versions")
    @Operation(summary = "查询某报名的所有作品版本")
    @PreAuthorize("hasAuthority('contest:read')")
    public Result<List<WorkVO>> getVersionsByRegistration(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long registrationId) {
        Long tenantId = userPrincipal.getTenantId();
        List<WorkVO> result = workService.getVersionsByRegistration(registrationId, tenantId);
        return Result.success(result);
    }

    @GetMapping("/guided")
    @Operation(summary = "查询教师指导的作品列表")
    public Result<Page<WorkVO>> getGuidedWorks(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @ModelAttribute WorkQueryDTO queryDTO) {
        Long userId = userPrincipal.getUserId();
        Long tenantId = userPrincipal.getTenantId();
        Page<WorkVO> result = workService.getGuidedWorks(queryDTO, userId, tenantId);
        return Result.success(result);
    }
}
