package org.xyzh.crontab.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.crontab.TaskMetaService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.crontab.TbCrontabTaskMeta;

/**
 * @description 定时任务元数据控制器
 * @filename TaskMetaController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-11-18
 */
@RestController
@RequestMapping("/crontab/meta")
public class TaskMetaController {

    private static final Logger logger = LoggerFactory.getLogger(TaskMetaController.class);

    @Autowired
    private TaskMetaService taskMetaService;

    /**
     * @description 创建任务元数据
     * @param taskMeta 任务元数据
     * @return ResultDomain<TbCrontabTaskMeta>
     */
    @PostMapping
    public ResultDomain<TbCrontabTaskMeta> createTaskMeta(@RequestBody TbCrontabTaskMeta taskMeta) {
        return taskMetaService.createTaskMeta(taskMeta);
    }

    /**
     * @description 更新任务元数据
     * @param taskMeta 任务元数据
     * @return ResultDomain<TbCrontabTaskMeta>
     */
    @PutMapping
    public ResultDomain<TbCrontabTaskMeta> updateTaskMeta(@RequestBody TbCrontabTaskMeta taskMeta) {
        return taskMetaService.updateTaskMeta(taskMeta);
    }

    /**
     * @description 删除任务元数据
     * @param metaId 元数据ID
     * @return ResultDomain<Boolean>
     */
    @DeleteMapping("/{metaId}")
    public ResultDomain<Boolean> deleteTaskMeta(@PathVariable String metaId) {
        return taskMetaService.deleteTaskMeta(metaId);
    }

    /**
     * @description 根据ID查询任务元数据
     * @param metaId 元数据ID
     * @return ResultDomain<TbCrontabTaskMeta>
     */
    @GetMapping("/{metaId}")
    public ResultDomain<TbCrontabTaskMeta> getTaskMetaById(@PathVariable String metaId) {
        return taskMetaService.getTaskMetaById(metaId);
    }

    /**
     * @description 查询所有任务元数据
     * @return ResultDomain<TbCrontabTaskMeta>
     */
    @GetMapping("/all")
    public ResultDomain<TbCrontabTaskMeta> getAllTaskMeta() {
        return taskMetaService.getAllTaskMeta();
    }

    /**
     * @description 根据分类查询任务元数据
     * @param category 分类
     * @return ResultDomain<TbCrontabTaskMeta>
     */
    @GetMapping("/category/{category}")
    public ResultDomain<TbCrontabTaskMeta> getTaskMetaByCategory(@PathVariable String category) {
        return taskMetaService.getTaskMetaByCategory(category);
    }

    /**
     * @description 分页查询任务元数据
     * @param pageRequest 分页请求
     * @return ResultDomain<TbCrontabTaskMeta>
     */
    @PostMapping("/page")
    public ResultDomain<TbCrontabTaskMeta> getTaskMetaPage(@RequestBody PageRequest<TbCrontabTaskMeta> pageRequest) {
        return taskMetaService.getTaskMetaPage(pageRequest);
    }
}
