package com.erp.print.controller;

import com.erp.print.dto.request.PrinterRequest;
import com.erp.common.core.model.ApiResponse;
import com.erp.common.core.model.PageResponse;
import com.erp.print.entity.Printer;
import com.erp.print.service.PrinterService;
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;
import java.util.List;

@Tag(name = "打印机管理")
@RestController
@RequestMapping("/api/print/printers")
@RequiredArgsConstructor
public class PrinterController {

    private final PrinterService printerService;

    @Operation(summary = "分页查询打印机")
    @GetMapping
    public ApiResponse<PageResponse<Printer>> listPrinters(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer limit,
            @RequestParam(required = false) String printerType,
            @RequestParam(required = false) String keyword) {
        return ApiResponse.success(printerService.listPrinters(page, limit, printerType, keyword));
    }

    @Operation(summary = "获取打印机详情")
    @GetMapping("/{id}")
    public ApiResponse<Printer> getPrinter(@PathVariable Long id) {
        return ApiResponse.success(printerService.getPrinterById(id));
    }

    @Operation(summary = "根据编码获取打印机")
    @GetMapping("/code/{printerCode}")
    public ApiResponse<Printer> getPrinterByCode(@PathVariable String printerCode) {
        return ApiResponse.success(printerService.getPrinterByCode(printerCode));
    }

    @Operation(summary = "获取可用打印机列表")
    @GetMapping("/available")
    public ApiResponse<List<Printer>> getAvailablePrinters() {
        return ApiResponse.success(printerService.getAvailablePrinters());
    }

    @Operation(summary = "创建打印机")
    @PostMapping
    public ApiResponse<Printer> createPrinter(@Valid @RequestBody PrinterRequest request) {
        return ApiResponse.success("创建成功", printerService.createPrinter(request));
    }

    @Operation(summary = "更新打印机")
    @PutMapping("/{id}")
    public ApiResponse<Printer> updatePrinter(@PathVariable Long id, @Valid @RequestBody PrinterRequest request) {
        request.setId(id);
        return ApiResponse.success("更新成功", printerService.updatePrinter(request));
    }

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

    @Operation(summary = "设置默认打印机")
    @PostMapping("/{id}/default")
    public ApiResponse<Void> setDefaultPrinter(@PathVariable Long id) {
        printerService.setDefaultPrinter(id);
        return ApiResponse.success("设置成功", null);
    }

    @Operation(summary = "更新打印机状态")
    @PutMapping("/{id}/status")
    public ApiResponse<Void> updatePrinterStatus(@PathVariable Long id, @RequestParam Integer status) {
        printerService.updatePrinterStatus(id, status);
        return ApiResponse.success("更新成功", null);
    }
}
