package com.fs.course.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.course.config.CourseRandomRedPacketConfig;
import com.fs.course.param.CourseRandomRedPacketBatchSaveParam;
import com.fs.course.param.CourseRandomRedPacketCopyParam;
import com.fs.course.service.ICourseRandomRedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程随机红包配置控制器，对应 fs_user_course.project
 */
@RestController
@RequestMapping("/course/courseRandomRedPacket")
public class CourseRandomRedPacketController extends BaseController {

    @Autowired
    private ICourseRandomRedPacketService courseRandomRedPacketService;

    /**
     * 获取课程随机红包配置
     */
    @PreAuthorize("@ss.hasPermi('course:courseRandomRedPacket:query')")
    @GetMapping("/{project}")
    public R getConfig(@PathVariable Long project) {
        CourseRandomRedPacketConfig config = courseRandomRedPacketService.getConfigByProject(project);
        return R.ok().put("data", config);
    }

    /**
     * 查询所有已配置的课程项目
     */
    @PreAuthorize("@ss.hasPermi('course:courseRandomRedPacket:query')")
    @GetMapping("/list")
    public R listConfiguredProjects() {
        List<CourseRandomRedPacketConfig> list = courseRandomRedPacketService.listConfiguredProjects();
        return R.ok().put("data", list);
    }

    /**
     * 修改/保存课程随机红包配置（单个项目）
     */
    @PreAuthorize("@ss.hasPermi('course:courseRandomRedPacket:edit')")
    @Log(title = "课程随机红包配置", businessType = BusinessType.UPDATE)
    @PostMapping
    public AjaxResult saveConfig(@RequestBody CourseRandomRedPacketConfig config) {
        courseRandomRedPacketService.saveConfig(config);
        return AjaxResult.success();
    }

    /**
     * 批量保存：同一套配置应用到多个课程项目
     */
    @PreAuthorize("@ss.hasPermi('course:courseRandomRedPacket:edit')")
    @Log(title = "课程随机红包批量配置", businessType = BusinessType.UPDATE)
    @PostMapping("/batch")
    public AjaxResult batchSaveConfig(@RequestBody CourseRandomRedPacketBatchSaveParam param) {
        courseRandomRedPacketService.batchSaveConfig(param);
        return AjaxResult.success();
    }

    /**
     * 复制配置：将源项目配置复制到多个目标项目
     */
    @PreAuthorize("@ss.hasPermi('course:courseRandomRedPacket:edit')")
    @Log(title = "课程随机红包配置复制", businessType = BusinessType.UPDATE)
    @PostMapping("/copy")
    public AjaxResult copyConfig(@RequestBody CourseRandomRedPacketCopyParam param) {
        courseRandomRedPacketService.copyConfig(param);
        return AjaxResult.success();
    }
}
