package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.LifePathCreateRequest;
import com.emotion.dto.request.LifePathPageRequest;
import com.emotion.dto.request.LifePathUpdateRequest;
import com.emotion.dto.response.LifePathResponse;
import com.emotion.service.LifePathService;
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("/lifePath")
public class LifePathController {

    @Autowired
    private LifePathService lifePathService;

    /**
     * 分页查询当前用户的实现路径
     */
    @GetMapping(value = "/page")
    public Result<PageResult<LifePathResponse>> getPage(@Validated LifePathPageRequest request) {
        PageResult<LifePathResponse> pageResult = lifePathService.getPageByCurrentUser(request);
        return Result.success(pageResult);
    }

    /**
     * 获取当前用户的所有实现路径列表
     */
    @GetMapping(value = "/listAll")
    public Result<List<LifePathResponse>> getList() {
        List<LifePathResponse> paths = lifePathService.getListByCurrentUser();
        return Result.success(paths);
    }

    /**
     * 根据剧本ID获取实现路径
     */
    @GetMapping(value = "/byScript")
    public Result<LifePathResponse> getByScriptId(@RequestParam String scriptId) {
        LifePathResponse path = lifePathService.getByScriptId(scriptId);
        if (path == null) {
            return Result.notFound("实现路径不存在");
        }
        return Result.success(path);
    }

    /**
     * 根据ID获取实现路径详情
     */
    @GetMapping(value = "/detail")
    public Result<LifePathResponse> getById(@RequestParam String id) {
        LifePathResponse path = lifePathService.getPathById(id);
        if (path == null) {
            return Result.notFound("实现路径不存在");
        }
        return Result.success(path);
    }

    /**
     * 创建实现路径
     */
    @PostMapping(value = "/create")
    public Result<LifePathResponse> create(@Valid @RequestBody LifePathCreateRequest request) {
        LifePathResponse path = lifePathService.createPath(request);
        if (path == null) {
            return Result.error("创建失败");
        }
        return Result.success(path);
    }

    /**
     * 更新实现路径
     */
    @PutMapping(value = "/update")
    public Result<LifePathResponse> update(@Valid @RequestBody LifePathUpdateRequest request) {
        LifePathResponse path = lifePathService.updatePath(request);
        if (path == null) {
            return Result.error("更新失败");
        }
        return Result.success(path);
    }

    /**
     * 删除实现路径
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = lifePathService.deletePath(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
