package com.campus.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.activity.common.Result;
import com.campus.activity.dto.request.ScanCheckInRequest;
import com.campus.activity.dto.request.TicketCheckInRequest;
import com.campus.activity.entity.CheckIn;
import com.campus.activity.service.CheckInService;
import com.campus.activity.vo.CheckInVO;
import com.campus.activity.vo.QrCodeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "签到模块", description = "活动签到相关接口")
@RestController
@RequestMapping("/api/v1/checkin")
@RequiredArgsConstructor
public class CheckInController {

    private final CheckInService checkInService;

    @Operation(summary = "生成签到二维码（管理员）")
    @PostMapping("/qrcode/{activityId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<QrCodeVO> generateQrCode(@PathVariable Long activityId) {
        QrCodeVO qrCodeVO = checkInService.generateQrCode(activityId);
        return Result.success(qrCodeVO);
    }

    @Operation(summary = "学生扫码签到")
    @PostMapping("/scan")
    public Result<CheckInVO> scanCheckIn(@Valid @RequestBody ScanCheckInRequest request) {
        CheckInVO checkInVO = checkInService.scanCheckIn(request);
        return Result.success("签到成功", checkInVO);
    }

    @Operation(summary = "管理员扫学生票签到")
    @PostMapping("/ticket")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<CheckInVO> ticketCheckIn(@Valid @RequestBody TicketCheckInRequest request) {
        CheckInVO checkInVO = checkInService.ticketCheckIn(request);
        return Result.success("签到成功", checkInVO);
    }

    @Operation(summary = "获取活动签到列表（管理员）")
    @GetMapping("/activity/{activityId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<IPage<CheckInVO>> getActivityCheckIns(
            @PathVariable Long activityId,
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size) {
        Page<CheckIn> page = new Page<>(current, size);
        IPage<CheckInVO> result = checkInService.getActivityCheckIns(page, activityId);
        return Result.success(result);
    }
}