package com.rj.controller;

import com.rj.entity.ScriptManagementMy;
import com.rj.service.IScriptManagementMyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 话术库管理前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/scriptManagementMy")
@Tag(name = "话术库管理(script_management_my)", description = "增删改查与分页")
public class ScriptManagementMyController {

    @Autowired
    private IScriptManagementMyService scriptManagementMyService;

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseEntity<Map<String, Object>> add(
            @Parameter(description = "实体", required = true) @RequestBody ScriptManagementMy entity) {
        Map<String, Object> result = scriptManagementMyService.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 ScriptManagementMy entity) {
        Map<String, Object> result = scriptManagementMyService.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", required = true) @PathVariable String id) {
        Map<String, Object> result = scriptManagementMyService.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 title,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String industry,
            @RequestParam(required = false) String creator,
            @RequestParam(required = false) String status) {
        Map<String, Object> result = scriptManagementMyService.pageQuery(
                current, size, title, category, industry, creator, status
        );
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.internalServerError().body(result);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "按ID查询")
    public ResponseEntity<Map<String, Object>> getById(
            @Parameter(description = "主键ID", required = true) @PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            ScriptManagementMy data = scriptManagementMyService.getById(id);
            if (data == null) {
                result.put("success", false);
                result.put("message", "记录不存在");
                return ResponseEntity.notFound().build();
            }
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", data);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("script_management_my get by id error, id={}", id, e);
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
}
