package com.competition.modules.pub.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.entity.BizContest;
import com.competition.modules.biz.contest.entity.BizContestRegistration;
import com.competition.modules.biz.contest.entity.BizContestWork;
import com.competition.modules.pub.dto.PublicRegisterActivityDto;
import com.competition.modules.pub.service.PublicActivityService;
import com.competition.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;

    @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));
    }

    @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));
    }
}
