package org.example.controller;

import org.example.entity.Patient;
import org.example.service.PatientService;
import org.example.util.PatientDataConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/patients")
public class PatientController {
    
    @Autowired
    private PatientService patientService;
    
    /**
     * 获取所有患者列表
     * @return 患者列表
     */
    @GetMapping
    public List<Patient> getAllPatients(
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String department,
        @RequestParam(required = false) String ward,
        @RequestParam(required = false) String roomNo,
        @RequestParam(required = false) String bedNo,
        @RequestParam(required = false) String inpatientNo,
        @RequestParam(required = false) String nurseLevel,
        @RequestParam(required = false) String status,
        @RequestParam(required = false) String admissionDateTime
    ) {
        List<Patient> patients;
        if (name != null || department != null || ward != null || roomNo != null || 
            bedNo != null || inpatientNo != null || nurseLevel != null || 
            status != null || admissionDateTime != null) {
            // 如果有任何查询参数，则执行带条件的查询
            patients = patientService.findWithConditions(name, department, ward, roomNo, bedNo, 
                                                     inpatientNo, nurseLevel, status, admissionDateTime);
        } else {
            // 否则返回所有患者
            patients = patientService.findAll();
        }
        // 转换患者数据中的代码为中文描述
        return PatientDataConverter.convertPatientListData(patients);
    }
    
    /**
     * 获取所有不同的护理级别
     * @return 护理级别列表
     */
    @GetMapping("/nurse-levels")
    public List<String> getDistinctNurseLevels() {
        List<String> nurseLevels = patientService.findDistinctNurseLevels();
        // 将护理级别代码转换为中文描述
        return nurseLevels.stream()
                .map(PatientDataConverter::convertNurseLevel)
                .distinct()
                .collect(Collectors.toList());
    }
    
    /**
     * 获取所有不同的在院状态
     * @return 在院状态列表
     */
    @GetMapping("/inpatient-statuses")
    public List<String> getDistinctInpatientStatuses() {
        List<String> inpatientStatuses = patientService.findDistinctInpatientStatuses();
        // 将在院状态代码转换为中文描述
        return inpatientStatuses.stream()
                .map(PatientDataConverter::convertInpatientStatus)
                .distinct()
                .collect(Collectors.toList());
    }
    
    /**
     * 根据住院号获取患者信息
     * @param inpatientNo 住院号
     * @return 患者信息
     */
    @GetMapping("/{inpatientNo}")
    public Map<String, Object> getPatientByInpatientNo(@PathVariable String inpatientNo) {
        Map<String, Object> result = new HashMap<>();
        try {
            Patient patient = patientService.findByInpatientNo(inpatientNo);
            if (patient != null) {
                // 转换患者数据中的代码为中文描述
                PatientDataConverter.convertPatientData(patient);
                result.put("success", true);
                result.put("data", patient);
            } else {
                result.put("success", false);
                result.put("message", "患者不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "获取患者信息失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 添加新患者
     * @param patient 患者信息
     * @return 操作结果
     */
    @PostMapping
    public Map<String, Object> addPatient(@RequestBody Patient patient) {
        Map<String, Object> result = new HashMap<>();
        try {
            patientService.addPatient(patient);
            result.put("success", true);
            result.put("message", "患者添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "患者添加失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新患者信息
     * @param patient 患者信息
     * @return 操作结果
     */
    @PutMapping
    public Map<String, Object> updatePatient(@RequestBody Patient patient) {
        Map<String, Object> result = new HashMap<>();
        try {
            patientService.updatePatient(patient);
            result.put("success", true);
            result.put("message", "患者信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "患者信息更新失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除患者
     * @param inpatientNo 住院号
     * @return 操作结果
     */
    @DeleteMapping("/{inpatientNo}")
    public Map<String, Object> deletePatient(@PathVariable String inpatientNo) {
        Map<String, Object> result = new HashMap<>();
        try {
            patientService.deletePatient(inpatientNo);
            result.put("success", true);
            result.put("message", "患者删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "患者删除失败: " + e.getMessage());
        }
        return result;
    }
}