package com.example.controller;

import com.example.common.Result;
import com.example.common.config.TokenUtils;
import com.example.entity.Account;
import com.example.entity.DoctorPatient;
import com.example.entity.Patient;
import com.example.service.DoctorPatientService;
import com.example.service.PatientService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 医生与患者关系控制器
 */
@RestController
@RequestMapping("/doctor-patient")
public class DoctorPatientController {

    @Resource
    private DoctorPatientService doctorPatientService;
    
    @Resource
    private PatientService patientService;

    /**
     * 新增医生患者关系
     * @param doctorPatient 医生患者关系信息
     * @return 结果
     */
    @PostMapping
    public Result add(@RequestBody DoctorPatient doctorPatient) {
        doctorPatientService.add(doctorPatient);
        return Result.success();
    }

    /**
     * 更新医生患者关系
     * @param doctorPatient 医生患者关系信息
     * @return 结果
     */
    @PutMapping
    public Result update(@RequestBody DoctorPatient doctorPatient) {
        doctorPatientService.update(doctorPatient);
        return Result.success();
    }

    /**
     * 删除医生患者关系
     * @param id 关系ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        doctorPatientService.delete(id);
        return Result.success();
    }

    /**
     * 结束医生患者关系
     * @param id 关系ID
     * @return 结果
     */
    @PutMapping("/end/{id}")
    public Result endRelationship(@PathVariable Integer id) {
        doctorPatientService.endRelationship(id);
        return Result.success();
    }

    /**
     * 获取当前医生的患者列表
     * @param name 患者姓名（可选）
     * @return 患者列表
     */
    @GetMapping("/doctor/patients")
    public Result getPatientsByDoctor(@RequestParam(required = false) String name) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null || !"doctor".equals(currentUser.getRole())) {
            return Result.error("401", "无权限");
        }
        
        List<DoctorPatient> list = doctorPatientService.selectByDoctorId(currentUser.getId(), name);
        return Result.success(list);
    }

    /**
     * 获取当前患者的医生列表
     * @param name 医生姓名（可选）
     * @return 医生列表
     */
    @GetMapping("/patient/doctors")
    public Result getDoctorsByPatient(@RequestParam(required = false) String name) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null || !"patient".equals(currentUser.getRole())) {
            return Result.error("401", "无权限");
        }
        
        List<DoctorPatient> list = doctorPatientService.selectByPatientId(currentUser.getId(), name);
        return Result.success(list);
    }
    
    /**
     * 根据医生ID获取其患者列表（管理员使用）
     * @param doctorId 医生ID
     * @param name 患者姓名（可选）
     * @return 患者列表
     */
    @GetMapping("/admin/doctor/{doctorId}/patients")
    public Result getPatientsByDoctorForAdmin(@PathVariable Integer doctorId, @RequestParam(required = false) String name) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("401", "无权限");
        }
        
        List<DoctorPatient> list = doctorPatientService.selectByDoctorId(doctorId, name);
        return Result.success(list);
    }
    
    /**
     * 根据患者ID获取其医生列表（管理员使用）
     * @param patientId 患者ID
     * @param name 医生姓名（可选）
     * @return 医生列表
     */
    @GetMapping("/admin/patient/{patientId}/doctors")
    public Result getDoctorsByPatientForAdmin(@PathVariable Integer patientId, @RequestParam(required = false) String name) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null || !"admin".equals(currentUser.getRole())) {
            return Result.error("401", "无权限");
        }
        
        List<DoctorPatient> list = doctorPatientService.selectByPatientId(patientId, name);
        return Result.success(list);
    }
} 