package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.EpicScriptCreateRequest;
import com.emotion.dto.request.EpicScriptPageRequest;
import com.emotion.dto.request.EpicScriptUpdateRequest;
import com.emotion.dto.response.EpicScriptResponse;
import com.emotion.service.EpicScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 爽文剧本控制器
 *
 * @author huazhongmin
 * @date 2025-12-22
 */
@RestController
@RequestMapping("/epicScript")
public class EpicScriptController {

    @Autowired
    private EpicScriptService epicScriptService;

    /**
     * 分页查询当前用户的爽文剧本
     */
    @GetMapping(value = "/page")
    public Result<PageResult<EpicScriptResponse>> getPage(@Validated EpicScriptPageRequest request) {
        PageResult<EpicScriptResponse> pageResult = epicScriptService.getPageByCurrentUser(request);
        return Result.success(pageResult);
    }

    /**
     * 获取当前用户的所有爽文剧本列表
     */
    @GetMapping(value = "/listAll")
    public Result<List<EpicScriptResponse>> getList() {
        List<EpicScriptResponse> scripts = epicScriptService.getListByCurrentUser();
        return Result.success(scripts);
    }

    /**
     * 根据ID获取爽文剧本详情
     */
    @GetMapping(value = "/detail")
    public Result<EpicScriptResponse> getById(@RequestParam String id) {
        EpicScriptResponse script = epicScriptService.getScriptById(id);
        if (script == null) {
            return Result.notFound("爽文剧本不存在");
        }
        return Result.success(script);
    }

    /**
     * 创建爽文剧本
     */
    @PostMapping(value = "/create")
    public Result<EpicScriptResponse> create(@Valid @RequestBody EpicScriptCreateRequest request) {
        EpicScriptResponse script = epicScriptService.createScript(request);
        if (script == null) {
            return Result.error("创建失败");
        }
        return Result.success(script);
    }

    /**
     * 更新爽文剧本
     */
    @PutMapping(value = "/update")
    public Result<EpicScriptResponse> update(@Valid @RequestBody EpicScriptUpdateRequest request) {
        EpicScriptResponse script = epicScriptService.updateScript(request);
        if (script == null) {
            return Result.error("更新失败");
        }
        return Result.success(script);
    }

    /**
     * 选中剧本（取消其他选中状态）
     */
    @PutMapping(value = "/select")
    public Result<EpicScriptResponse> select(@RequestParam String id) {
        EpicScriptResponse script = epicScriptService.selectScript(id);
        if (script == null) {
            return Result.error("选中失败");
        }
        return Result.success(script);
    }

    /**
     * 删除爽文剧本
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = epicScriptService.deleteScript(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
