package com.erp.print.controller;

import com.erp.print.dto.request.PrintConfigRequest;
import com.erp.common.core.model.ApiResponse;
import com.erp.print.entity.PrintConfig;
import com.erp.print.service.PrintConfigService;
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/configs")
@RequiredArgsConstructor
public class PrintConfigController {

    private final PrintConfigService configService;

    @Operation(summary = "获取所有配置")
    @GetMapping
    public ApiResponse<List<PrintConfig>> listConfigs() {
        return ApiResponse.success(configService.listConfigs());
    }

    @Operation(summary = "获取配置值")
    @GetMapping("/key/{configKey}")
    public ApiResponse<PrintConfig> getConfigByKey(@PathVariable String configKey) {
        return ApiResponse.success(configService.getConfigByKey(configKey));
    }

    @Operation(summary = "创建配置")
    @PostMapping
    public ApiResponse<PrintConfig> createConfig(@Valid @RequestBody PrintConfigRequest request) {
        return ApiResponse.success("创建成功", configService.createConfig(request));
    }

    @Operation(summary = "更新配置")
    @PutMapping("/{id}")
    public ApiResponse<PrintConfig> updateConfig(@PathVariable Long id, @Valid @RequestBody PrintConfigRequest request) {
        request.setId(id);
        return ApiResponse.success("更新成功", configService.updateConfig(request));
    }

    @Operation(summary = "删除配置")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteConfig(@PathVariable Long id) {
        configService.deleteConfig(id);
        return ApiResponse.success("删除成功", null);
    }
}
