package com.erp.print.controller;

import com.erp.print.dto.request.PrintTaskQueryRequest;
import com.erp.print.dto.request.PrintTaskRequest;
import com.erp.common.core.model.ApiResponse;
import com.erp.common.core.model.PageResponse;
import com.erp.print.entity.PrintTask;
import com.erp.print.service.PrintTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@Tag(name = "打印任务管理")
@RestController
@RequestMapping("/api/print/tasks")
@RequiredArgsConstructor
public class PrintTaskController {

    private final PrintTaskService taskService;

    @Operation(summary = "分页查询打印任务")
    @GetMapping
    public ApiResponse<PageResponse<PrintTask>> listTasks(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer limit,
            PrintTaskQueryRequest request) {
        return ApiResponse.success(taskService.listTasks(page, limit, request));
    }

    @Operation(summary = "获取任务详情")
    @GetMapping("/{id}")
    public ApiResponse<PrintTask> getTask(@PathVariable Long id) {
        return ApiResponse.success(taskService.getTaskById(id));
    }

    @Operation(summary = "根据任务编号获取任务")
    @GetMapping("/no/{taskNo}")
    public ApiResponse<PrintTask> getTaskByTaskNo(@PathVariable String taskNo) {
        return ApiResponse.success(taskService.getTaskByTaskNo(taskNo));
    }

    @Operation(summary = "创建打印任务")
    @PostMapping
    public ApiResponse<PrintTask> createTask(@Valid @RequestBody PrintTaskRequest request) {
        return ApiResponse.success("创建成功", taskService.createTask(request));
    }

    @Operation(summary = "取消打印任务")
    @PostMapping("/{id}/cancel")
    public ApiResponse<Void> cancelTask(@PathVariable Long id) {
        taskService.cancelTask(id);
        return ApiResponse.success("取消成功", null);
    }

    @Operation(summary = "重试打印任务")
    @PostMapping("/{id}/retry")
    public ApiResponse<Void> retryTask(@PathVariable Long id) {
        taskService.retryTask(id);
        return ApiResponse.success("重试成功", null);
    }

    @Operation(summary = "删除打印任务")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteTask(@PathVariable Long id) {
        taskService.deleteTask(id);
        return ApiResponse.success("删除成功", null);
    }
}
