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.RegistrationRequest;
import com.campus.activity.entity.Registration;
import com.campus.activity.service.RegistrationService;
import com.campus.activity.vo.RegistrationVO;
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.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "报名模块", description = "活动报名相关接口")
@RestController
@RequestMapping("/api/v1/registrations")
@RequiredArgsConstructor
public class RegistrationController {

    private final RegistrationService registrationService;

    @Operation(summary = "报名活动")
    @PostMapping
    public Result<RegistrationVO> register(@Valid @RequestBody RegistrationRequest request) {
        RegistrationVO registrationVO = registrationService.register(request);
        return Result.success("报名成功", registrationVO);
    }

    @Operation(summary = "取消报名")
    @DeleteMapping("/{id}")
    public Result<Void> cancelRegistration(@PathVariable Long id) {
        registrationService.cancelRegistration(id);
        return Result.success("取消成功", null);
    }

    @Operation(summary = "获取我的报名列表")
    @GetMapping("/my")
    public Result<IPage<RegistrationVO>> getMyRegistrations(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Integer status) {
        Page<Registration> page = new Page<>(current, size);
        IPage<RegistrationVO> result = registrationService.getMyRegistrations(page, status);
        return Result.success(result);
    }

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

    @Operation(summary = "下载电子票PDF")
    @GetMapping("/{id}/ticket")
    public ResponseEntity<byte[]> downloadTicket(@PathVariable Long id) {
        byte[] pdfBytes = registrationService.generateTicketPdf(id);

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=ticket.pdf")
                .contentType(MediaType.APPLICATION_PDF)
                .contentLength(pdfBytes.length)
                .body(pdfBytes);
    }
}