package com.xindazhou.business.controller.admin.community.activity;

import com.xindazhou.business.controller.admin.community.activity.vo.ActivityPageReqVO;
import com.xindazhou.business.controller.admin.community.activity.vo.ActivityRespVO;
import com.xindazhou.business.controller.admin.community.activity.vo.ActivitySaveReqVO;
import com.xindazhou.business.controller.admin.community.activity.vo.ActivityVirtualUserReqVO;
import com.xindazhou.business.service.community.activity.ActivityService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 活动管理 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 活动管理")
@RestController("communityActivityController")
@RequestMapping("/admin/community/activity")
@Validated
@Slf4j
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @GetMapping("/page")
    @Operation(summary = "获得活动分页列表")
    @PreAuthorize("@ss.hasPermission('community:activity:query')")
    public CommonResult<PageResult<ActivityRespVO>> getActivityPage(@Valid ActivityPageReqVO pageReqVO) {
        PageResult<ActivityRespVO> pageResult = activityService.getActivityPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/{activityId}")
    @Operation(summary = "获得活动")
    @Parameter(name = "activityId", description = "活动编号", required = true, example = "ACT001")
    @PreAuthorize("@ss.hasPermission('community:activity:query')")
    public CommonResult<ActivityRespVO> getActivity(@PathVariable("activityId") String activityId) {
        ActivityRespVO activity = activityService.getActivityByActivityId(activityId);
        return success(activity);
    }

    @PostMapping
    @Operation(summary = "创建活动")
    @PreAuthorize("@ss.hasPermission('community:activity:create')")
    public CommonResult<Long> createActivity(@Valid @RequestBody ActivitySaveReqVO createReqVO) {
        Long id = activityService.createActivity(createReqVO);
        return success(id);
    }

    @PutMapping("/{activityId}")
    @Operation(summary = "更新活动")
    @Parameter(name = "activityId", description = "活动编号", required = true, example = "ACT001")
    @PreAuthorize("@ss.hasPermission('community:activity:update')")
    public CommonResult<Boolean> updateActivity(@PathVariable("activityId") String activityId,
                                                @Valid @RequestBody ActivitySaveReqVO updateReqVO) {
        ActivityRespVO existingActivity = activityService.getActivityByActivityId(activityId);
        updateReqVO.setId(existingActivity.getId());
        activityService.updateActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/{activityId}")
    @Operation(summary = "删除活动")
    @Parameter(name = "activityId", description = "活动编号", required = true, example = "ACT001")
    @PreAuthorize("@ss.hasPermission('community:activity:delete')")
    public CommonResult<Boolean> deleteActivity(@PathVariable("activityId") String activityId) {
        ActivityRespVO existingActivity = activityService.getActivityByActivityId(activityId);
        activityService.deleteActivity(existingActivity.getId());
        return success(true);
    }

    @PostMapping("/{activityId}/virtual-users")
    @Operation(summary = "设置虚拟用户")
    @Parameter(name = "activityId", description = "活动编号", required = true, example = "ACT001")
    @PreAuthorize("@ss.hasPermission('community:activity:update')")
    public CommonResult<Boolean> setVirtualUsers(@PathVariable("activityId") String activityId,
                                                 @Valid @RequestBody ActivityVirtualUserReqVO reqVO) {
        activityService.setVirtualUsers(activityId, reqVO.getVirtualCount());
        return success(true);
    }

}

