package com.rj.controller;

import com.rj.entity.LbDeductionAmount;
import com.rj.service.ILbDeductionAmountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/lbDeductionAmount")
@Tag(name = "抵扣金记录", description = "lb_deduction_amount 增删改查与分页")
public class LbDeductionAmountController {

    @Autowired
    private ILbDeductionAmountService lbDeductionAmountService;

    @Data
    @Schema(description = "大模型解析抵扣金信息请求体")
    public static class ParseDeductionTextRequest {
        @Schema(description = "租户ID", requiredMode = Schema.RequiredMode.REQUIRED)
        @NotBlank(message = "tenantId不能为空")
        private String tenantId;

        @Schema(description = "抵扣金相关自然语言原文", requiredMode = Schema.RequiredMode.REQUIRED)
        @NotBlank(message = "文本不能为空")
        private String text;
    }

    @PostMapping("/parseFromText")
    @Operation(summary = "大模型解析抵扣金文本并保存", description = "根据自然语言调用大模型抽取字段为 LbDeductionAmount，落库后返回")
    public ResponseEntity<Map<String, Object>> parseFromText(
            @Parameter(description = "租户ID与自然语言文本", required = true) @Valid @RequestBody ParseDeductionTextRequest body) {
        Map<String, Object> result = lbDeductionAmountService.parseFromTextByLlmAndSave(
                body.getTenantId(), body.getText());
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseEntity<Map<String, Object>> add(
            @Parameter(description = "实体（id 为空时自动生成 UUID）", required = true)
            @RequestBody LbDeductionAmount entity) {
        Map<String, Object> result = lbDeductionAmountService.add(entity);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @PutMapping("/update")
    @Operation(summary = "编辑")
    public ResponseEntity<Map<String, Object>> update(
            @Parameter(description = "实体", required = true) @RequestBody LbDeductionAmount entity) {
        Map<String, Object> result = lbDeductionAmountService.update(entity);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    public ResponseEntity<Map<String, Object>> delete(
            @Parameter(description = "主键ID(UUID)", required = true) @PathVariable String id) {
        Map<String, Object> result = lbDeductionAmountService.deleteById(id);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询")
    public ResponseEntity<Map<String, Object>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String tenantId,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String userPhone,
            @Parameter(description = "创建时间起，格式 yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) String createTimeStart,
            @Parameter(description = "创建时间止，格式 yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) String createTimeEnd) {
        Map<String, Object> result = lbDeductionAmountService.pageQuery(
                current, size, tenantId, userName, userPhone, createTimeStart, createTimeEnd
        );
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.internalServerError().body(result);
    }



}
