package org.example.controller;

import org.example.entity.HospitalDept;
import org.example.service.HospitalDeptService;
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/departments")
public class HospitalDeptController {
    
    @Autowired
    private HospitalDeptService hospitalDeptService;
    
    /**
     * 获取所有科室列表（支持按名称模糊查询和启用状态筛选）
     * @param name 科室名称（模糊匹配）
     * @param enabled 是否启用
     * @return 科室列表
     */
    @GetMapping
    public List<HospitalDept> getAllDepartments(@RequestParam(required = false) String name, 
                                               @RequestParam(required = false) Boolean enabled) {
        List<HospitalDept> allDepts = hospitalDeptService.findAll();
        
        // 如果提供了名称参数，则进行模糊匹配
        if (name != null && !name.isEmpty()) {
            allDepts = allDepts.stream()
                    .filter(dept -> dept.getName() != null && dept.getName().contains(name))
                    .collect(Collectors.toList());
        }
        
        // 如果提供了启用状态参数，则进行筛选
        if (enabled != null) {
            allDepts = allDepts.stream()
                    .filter(dept -> enabled.equals(dept.getEnabled()))
                    .collect(Collectors.toList());
        }
        
        return allDepts;
    }
    
    /**
     * 根据科室代码获取科室信息
     * @param code 科室代码
     * @return 科室信息
     */
    @GetMapping("/{code}")
    public Map<String, Object> getDepartmentByCode(@PathVariable String code) {
        Map<String, Object> result = new HashMap<>();
        try {
            HospitalDept dept = hospitalDeptService.findByCode(code);
            if (dept != null) {
                result.put("success", true);
                result.put("data", dept);
            } else {
                result.put("success", false);
                result.put("message", "未找到该科室信息");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取科室信息失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 添加新科室
     * @param dept 科室信息
     * @return 操作结果
     */
    @PostMapping
    public Map<String, Object> addDepartment(@RequestBody HospitalDept dept) {
        Map<String, Object> result = new HashMap<>();
        try {
            hospitalDeptService.addDept(dept);
            result.put("success", true);
            result.put("message", "科室添加成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "科室添加失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新科室信息
     * @param code 科室代码
     * @param dept 科室信息
     * @return 操作结果
     */
    @PutMapping("/{code}")
    public Map<String, Object> updateDepartment(@PathVariable String code, @RequestBody HospitalDept dept) {
        Map<String, Object> result = new HashMap<>();
        try {
            dept.setCode(code);
            hospitalDeptService.updateDept(dept);
            result.put("success", true);
            result.put("message", "科室更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "科室更新失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除科室
     * @param code 科室代码
     * @return 操作结果
     */
    @DeleteMapping("/{code}")
    public Map<String, Object> deleteDepartment(@PathVariable String code) {
        Map<String, Object> result = new HashMap<>();
        try {
            hospitalDeptService.deleteDept(code);
            result.put("success", true);
            result.put("message", "科室删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "科室删除失败: " + e.getMessage());
        }
        return result;
    }
}