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.BizContestRegistration;
import com.competition.modules.pub.service.PublicActivityService;
import com.competition.modules.pub.service.PublicInteractionService;
import com.competition.modules.pub.service.PublicProfileService;
import com.competition.modules.pub.service.PublicUserWorkService;
import com.competition.modules.ugc.entity.UgcWork;
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/mine")
@RequiredArgsConstructor
public class PublicProfileController {

    private final PublicProfileService publicProfileService;
    private final PublicUserWorkService publicUserWorkService;
    private final PublicInteractionService publicInteractionService;

    @GetMapping("/profile")
    @Operation(summary = "获取个人资料")
    public Result<Map<String, Object>> getProfile() {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.getProfile(userId));
    }

    @PutMapping("/profile")
    @Operation(summary = "更新个人资料")
    public Result<Void> updateProfile(@RequestBody Map<String, String> body) {
        Long userId = SecurityUtil.getCurrentUserId();
        publicProfileService.updateProfile(userId,
                body.get("nickname"), body.get("city"), body.get("avatar"), body.get("gender"));
        return Result.success();
    }

    @GetMapping("/registrations")
    @Operation(summary = "我的报名列表")
    public Result<PageResult<UgcWork>> getRegistrations(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        // Reuse user work service for simplicity - returns user's works
        return Result.success(publicUserWorkService.findMyWorks(userId, page, pageSize, null, null));
    }

    @GetMapping("/works")
    @Operation(summary = "我的作品列表")
    public Result<PageResult<UgcWork>> getWorks(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicUserWorkService.findMyWorks(userId, page, pageSize, null, null));
    }

    @GetMapping("/favorites")
    @Operation(summary = "我的收藏列表")
    public Result<PageResult<Map<String, Object>>> getFavorites(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicInteractionService.getMyFavorites(userId, page, pageSize));
    }

    @GetMapping("/parent-info")
    @Operation(summary = "获取家长信息（子女视角）")
    public Result<Map<String, Object>> getParentInfo() {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicProfileService.getParentInfo(userId));
    }
}
