package com.lesingle.modules.pub.controller;

import com.lesingle.common.result.PageResult;
import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.biz.contest.entity.BizContest;
import com.lesingle.modules.biz.contest.entity.BizContestRegistration;
import com.lesingle.modules.biz.contest.entity.BizContestWork;
import com.lesingle.modules.biz.review.service.IContestResultService;
import com.lesingle.modules.pub.dto.PublicRegisterActivityDto;
import com.lesingle.modules.pub.service.PublicActivityService;
import com.lesingle.security.annotation.Public;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "活动大厅")
@RestController
@RequestMapping("/public/activities")
@RequiredArgsConstructor
public class PublicActivityController {

    private final PublicActivityService publicActivityService;
    private final IContestResultService contestResultService;

    @Public
    @GetMapping
    @Operation(summary = "活动列表")
    public Result<PageResult<BizContest>> listActivities(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String contestType) {
        return Result.success(publicActivityService.listActivities(page, pageSize, keyword, contestType));
    }

    @Public
    @GetMapping("/{id}")
    @Operation(summary = "活动详情")
    public Result<Map<String, Object>> getActivityDetail(@PathVariable Long id) {
        return Result.success(publicActivityService.getActivityDetail(id));
    }

    @Public
    @GetMapping("/{id}/results")
    @Operation(summary = "公示成果列表（仅公开活动且成果已发布，无需登录）")
    public Result<PageResult<Map<String, Object>>> getPublishedResults(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "10") Long pageSize) {
        return Result.success(contestResultService.getPublicResults(id, page, pageSize));
    }

    @Public
    @GetMapping("/{id}/works")
    @Operation(summary = "参赛作品列表（公开活动已发布，无需登录）")
    public Result<PageResult<Map<String, Object>>> listPublicWorks(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(publicActivityService.listPublicContestWorks(id, page, pageSize));
    }

    @GetMapping("/{id}/my-registration")
    @Operation(summary = "查询我的报名信息")
    public Result<Map<String, Object>> getMyRegistration(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicActivityService.getMyRegistration(id, userId));
    }

    @GetMapping("/mine/registrations")
    @Operation(summary = "我的报名列表")
    public Result<Map<String, Object>> getMyRegistrations(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicActivityService.getMyRegistrations(userId, page, pageSize));
    }

    @PostMapping("/{id}/register")
    @Operation(summary = "报名活动")
    public Result<BizContestRegistration> register(
            @PathVariable Long id,
            @RequestBody PublicRegisterActivityDto dto) {
        Long userId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(publicActivityService.register(id, userId, tenantId, dto));
    }

    @PostMapping("/{id}/submit-work")
    @Operation(summary = "提交作品")
    public Result<BizContestWork> submitWork(
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        Long userId = SecurityUtil.getCurrentUserId();
        Long tenantId = SecurityUtil.getCurrentTenantId();
        return Result.success(publicActivityService.submitWork(id, userId, tenantId, body));
    }
}
