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.*;
import com.campus.activity.entity.Activity;
import com.campus.activity.service.ActivityService;
import com.campus.activity.vo.ActivityVO;
import com.campus.activity.vo.ConflictCheckVO;
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.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "活动模块", description = "活动管理相关接口")
@RestController
@RequestMapping("/api/v1/activities")
@RequiredArgsConstructor
public class ActivityController {

    private final ActivityService activityService;

    @Operation(summary = "获取活动列表")
    @GetMapping
    public Result<IPage<ActivityVO>> pageActivities(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate) {
        Page<Activity> page = new Page<>(current, size);
        IPage<ActivityVO> result = activityService.pageActivities(page, status, keyword, category, startDate, endDate);
        return Result.success(result);
    }

    @Operation(summary = "获取活动详情")
    @GetMapping("/{id}")
    public Result<ActivityVO> getActivityById(@PathVariable Long id) {
        ActivityVO activityVO = activityService.getActivityById(id);
        return Result.success(activityVO);
    }

    @Operation(summary = "创建活动（管理员）")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Long> createActivity(@Valid @RequestBody ActivityCreateRequest request) {
        Long activityId = activityService.createActivity(request);
        return Result.success("创建成功", activityId);
    }

    @Operation(summary = "更新活动（管理员）")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateActivity(@PathVariable Long id, @Valid @RequestBody ActivityUpdateRequest request) {
        activityService.updateActivity(id, request);
        return Result.success("更新成功", null);
    }

    @Operation(summary = "删除活动（管理员）")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> deleteActivity(@PathVariable Long id) {
        activityService.deleteActivity(id);
        return Result.success("删除成功", null);
    }

    @Operation(summary = "获取日历视图活动")
    @GetMapping("/calendar")
    public Result<List<ActivityVO>> getCalendarActivities(
            @RequestParam Integer year,
            @RequestParam Integer month) {
        List<ActivityVO> activities = activityService.getCalendarActivities(year, month);
        return Result.success(activities);
    }

    @Operation(summary = "检测时间冲突")
    @PostMapping("/check-conflict")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<ConflictCheckVO> checkConflict(@Valid @RequestBody CheckConflictRequest request) {
        ConflictCheckVO result = activityService.checkConflict(request);
        return Result.success(result);
    }
}