package com.zhentao.controller;

import com.zhentao.common.Result;
import com.zhentao.entity.ActivityRegistration;
import com.zhentao.service.ActivityRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动报名记录控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/activity-registration")
@CrossOrigin(origins = "*")
public class ActivityRegistrationController {
    
    private final ActivityRegistrationService activityRegistrationService;
    
    public ActivityRegistrationController(ActivityRegistrationService activityRegistrationService) {
        this.activityRegistrationService = activityRegistrationService;
    }
    
    /**
     * 获取用户的活动报名记录
     */
    @GetMapping("/my-activities")
    public Result<List<ActivityRegistration>> getMyActivities(
            @RequestParam Integer userId,
            @RequestParam(required = false) Integer status) {
        
        log.info("查询用户活动报名记录: userId={}, status={}", userId, status);
        
        try {
            List<ActivityRegistration> registrations = activityRegistrationService.getUserRegistrations(userId, status);
            return Result.success(registrations);
        } catch (Exception e) {
            log.error("查询用户活动报名记录失败", e);
            return Result.error("查询失败");
        }
    }
    
    /**
     * 取消报名
     */
    @PutMapping("/cancel")
    public Result<String> cancelRegistration(
            @RequestParam Long id,
            @RequestParam Integer userId) {
        
        log.info("取消活动报名: id={}, userId={}", id, userId);
        
        try {
            boolean success = activityRegistrationService.cancelRegistration(id, userId);
            if (success) {
                return Result.success("取消报名成功");
            } else {
                return Result.error("取消报名失败，记录不存在或已取消");
            }
        } catch (Exception e) {
            log.error("取消报名失败", e);
            return Result.error("取消报名失败");
        }
    }
}


