package com.rj.controller;

import com.rj.entity.LbAssessmentApply;
import com.rj.service.ILbAssessmentApplyService;
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.servlet.http.HttpServletResponse;
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/lbAssessmentApply")
@Tag(name = "申请评估", description = "lb_assessment_apply增删改查与分页")
public class LbAssessmentApplyController {

    @Autowired
    private ILbAssessmentApplyService lbAssessmentApplyService;

    @Data
    @Schema(description = "大模型解析申请评估信息请求体")
    public static class ParseAssessmentTextRequest {
        @Schema(description = "申请评估原文", requiredMode = Schema.RequiredMode.REQUIRED)
        @NotBlank(message = "申请评估文本不能为空")
        private String assessmentText;

        @Schema(description = "当前登录人租户ID")
        private String tenantId;
    }

    @PostMapping("/parseAssessmentApplyFromText")
    @Operation(summary = "大模型解析申请评估文本为实体", description = "根据自然语言申请评估信息调用大模型抽取字段，返回 LbAssessmentApply（不落库）")
    public ResponseEntity<Map<String, Object>> parseAssessmentApplyFromText(
            @Parameter(description = "申请评估文本与租户ID", required = true) @Valid @RequestBody ParseAssessmentTextRequest body) {
        Map<String, Object> result = lbAssessmentApplyService.parseFromAssessmentTextByLlm(
                body.getAssessmentText(), body.getTenantId());
        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 = "实体", required = true) @RequestBody LbAssessmentApply entity) {
        Map<String, Object> result = lbAssessmentApplyService.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 LbAssessmentApply entity) {
        Map<String, Object> result = lbAssessmentApplyService.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 = lbAssessmentApplyService.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 applicantName,
            @RequestParam(required = false) String applicantPhone,
            @RequestParam(required = false) String colleagueName,
            @RequestParam(required = false) String teamLeaderName,
            @RequestParam(required = false) String teamBigLeaderName,
            @RequestParam(required = false) String assessmentTeacherName,
            @RequestParam(required = false) String moderatorName,
            @RequestParam(required = false) String meetingNumber) {
        Map<String, Object> result = lbAssessmentApplyService.pageQuery(
                current, size, tenantId, applicantName, applicantPhone, colleagueName, teamLeaderName,
                teamBigLeaderName, assessmentTeacherName, moderatorName, meetingNumber
        );
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.internalServerError().body(result);
    }

    @GetMapping("/notificationText")
    @Operation(summary = "生成评估会议通知文案", description = "根据租户ID与记录ID查询申请评估记录，拼接通知文本并写入 notification_text 字段后返回")
    public ResponseEntity<Map<String, Object>> getNotificationText(
            @Parameter(description = "租户ID", required = true) @RequestParam String tenantId,
            @Parameter(description = "申请评估记录ID(UUID)", required = true) @RequestParam String id) {
        Map<String, Object> result = lbAssessmentApplyService.buildAssessmentNotificationText(tenantId, id);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @PostMapping("/sendNotificationToDingTalk")
    @Operation(summary = "发送评估会议通知到钉钉群", description = "根据租户ID与记录ID查询申请评估记录，将 notification_text 发送至配置的钉钉群机器人")
    public ResponseEntity<Map<String, Object>> sendNotificationToDingTalk(
            @Parameter(description = "租户ID", required = true) @RequestParam String tenantId,
            @Parameter(description = "申请评估记录ID(UUID)", required = true) @RequestParam String id) {
        Map<String, Object> result = lbAssessmentApplyService.sendNotificationTextToDingTalk(tenantId, id);
        Boolean success = (Boolean) result.get("success");
        if (success != null && success) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body(result);
    }

    @GetMapping("/export")
    @Operation(summary = "导出申请评估Excel", description = "按租户ID与申请日期时间范围导出申请评估列表")
    public void export(
            @Parameter(description = "租户ID", required = true) @RequestParam String tenantId,
            @Parameter(description = "申请开始时间，格式：yyyy-MM-dd HH:mm:ss", required = true)
            @RequestParam String applicationDatetimeStart,
            @Parameter(description = "申请结束时间，格式：yyyy-MM-dd HH:mm:ss", required = true)
            @RequestParam String applicationDatetimeEnd,
            HttpServletResponse response) {
        lbAssessmentApplyService.exportExcel(
                tenantId, applicationDatetimeStart, applicationDatetimeEnd, response
        );
    }

}
