package com.example.controller;

import com.example.common.Result;
import com.example.common.config.TokenUtils;
import com.example.entity.Account;
import com.example.entity.Doctor;
import com.example.entity.DoctorPatient;
import com.example.service.DoctorPatientService;
import com.example.service.DoctorService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生相关接口
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Resource
    private DoctorService doctorService;
    
    @Resource
    private DoctorPatientService doctorPatientService;

    /**
     * 添加医生
     */
    @PostMapping
    public Result add(@RequestBody Doctor doctor) {
        doctorService.add(doctor);
        return Result.success();
    }

    /**
     * 删除医生
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        doctorService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除医生
     */
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        doctorService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改医生
     */
    @PutMapping
    public Result update(@RequestBody Doctor doctor) {
        doctorService.updateById(doctor);
        return Result.success();
    }

    /**
     * 根据ID查询医生
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Doctor doctor = doctorService.selectById(id);
        return Result.success(doctor);
    }

    /**
     * 查询所有医生
     */
    @GetMapping
    public Result getAll(Doctor doctor) {
        List<Doctor> list = doctorService.selectAll(doctor);
        return Result.success(list);
    }

    /**
     * 分页查询医生
     */
    @GetMapping("/page")
    public Result page(Doctor doctor, 
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Doctor> page = doctorService.selectPage(doctor, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 分页查询医生（与Admin页面保持一致的接口）
     */
    @GetMapping("/selectPage")
    public Result selectPage(
            Doctor doctor,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Doctor> pageInfo = doctorService.selectPage(doctor, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 获取当前医生负责的患者列表
     * 此接口是为了兼容之前的路径
     * @param name 患者姓名（可选）
     * @return 患者列表
     */
    @GetMapping("/myPatients")
    public Result myPatients(@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);
    }

    /**
     * 更新医生个人资料
     */
    @PutMapping("/update")
    public Result updateProfile(@RequestBody Doctor doctor) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null || !"doctor".equals(currentUser.getRole())) {
            return Result.error("401", "无权限");
        }
        
        // 确保只能更新自己的信息
        if (!currentUser.getId().equals(doctor.getId())) {
            return Result.error("403", "只能更新自己的信息");
        }
        
        // 更新个人资料
        doctorService.updateById(doctor);
        
        return Result.success();
    }
} 