package com.example.controller;

import com.example.common.Result;
import com.example.common.config.TokenUtils;
import com.example.entity.Account;
import com.example.entity.AssessmentReport;
import com.example.service.AssessmentReportService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 评估报告控制器
 */
@RestController
@RequestMapping("/assessmentReport")
public class AssessmentReportController {

    @Resource
    private AssessmentReportService assessmentReportService;

    /**
     * 手动生成评估报告
     */
    @PostMapping("/generate")
    public Result generate(@RequestBody Map<String, Object> params) {
        Integer patientId = params.get("patientId") != null ? Integer.valueOf(params.get("patientId").toString()) : null;
        String reportPeriod = params.get("reportPeriod") != null ? params.get("reportPeriod").toString() : null;
        
        if (patientId == null) {
            return Result.error("400", "缺少必要参数patientId");
        }
        if (reportPeriod == null) {
            return Result.error("400", "缺少必要参数reportPeriod");
        }
        
        AssessmentReport report = assessmentReportService.generateReport(patientId, reportPeriod);
        return Result.success(report);
    }
    
    /**
     * 获取患者的评估报告列表
     */
    @GetMapping("/patient/{patientId}")
    public Result getReportsByPatientId(@PathVariable Integer patientId) {
        List<AssessmentReport> reports = assessmentReportService.getReportsByPatientId(patientId);
        return Result.success(reports);
    }
    
    /**
     * 获取医生负责的患者的评估报告列表
     */
    @GetMapping("/doctor")
    public Result getReportsByDoctor() {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            return Result.error("401", "未登录");
        }
        
        List<AssessmentReport> reports = assessmentReportService.getReportsByDoctorId(currentUser.getId());
        return Result.success(reports);
    }
    
    /**
     * 获取所有评估报告列表（管理员使用）
     */
    @GetMapping("/admin")
    public Result getAllReports() {
        List<AssessmentReport> reports = assessmentReportService.getAllReports();
        return Result.success(reports);
    }
    
    /**
     * 添加医生批注
     */
    @PutMapping("/comment/{reportId}")
    public Result addComment(@PathVariable Integer reportId, @RequestParam(required = false) String comment) {
        if (comment == null || comment.isEmpty()) {
            return Result.error("400", "评论内容不能为空");
        }
        assessmentReportService.addDoctorComment(reportId, comment);
        return Result.success();
    }
    
    /**
     * 更新报告状态为已读
     */
    @PutMapping("/read/{reportId}")
    public Result markAsRead(@PathVariable Integer reportId) {
        assessmentReportService.markAsRead(reportId);
        return Result.success();
    }
    
    /**
     * 获取评估报告详情
     */
    @GetMapping("/{reportId}")
    public Result getReportById(@PathVariable Integer reportId) {
        // 获取当前用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            return Result.error("401", "未登录");
        }
        
        // 通过ID获取报告
        AssessmentReport report = assessmentReportService.getReportById(reportId);
        
        if (report == null) {
            return Result.error("404", "报告不存在");
        }
        
        return Result.success(report);
    }
} 