package com.nursinghome.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.nursinghome.common.ApiResponse;
import com.nursinghome.dto.CareRecordRequest;
import com.nursinghome.dto.HandoverRequest;
import com.nursinghome.dto.HealthRecordRequest;
import com.nursinghome.entity.*;
import com.nursinghome.mapper.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/nurse")
@SaCheckRole("NURSE")
public class NurseController {
    private final ScheduleMapper scheduleMapper;
    private final CareRecordMapper careRecordMapper;
    private final HealthRecordMapper healthRecordMapper;
    private final HandoverMapper handoverMapper;
    private final NoticeMapper noticeMapper;

    public NurseController(ScheduleMapper scheduleMapper,
                           CareRecordMapper careRecordMapper,
                           HealthRecordMapper healthRecordMapper,
                           HandoverMapper handoverMapper,
                           NoticeMapper noticeMapper) {
        this.scheduleMapper = scheduleMapper;
        this.careRecordMapper = careRecordMapper;
        this.healthRecordMapper = healthRecordMapper;
        this.handoverMapper = handoverMapper;
        this.noticeMapper = noticeMapper;
    }

    @GetMapping("/schedules")
    public ApiResponse<List<Schedule>> listSchedules(@RequestParam String date) {
        Long nurseId = Long.valueOf(StpUtil.getLoginId().toString());
        return ApiResponse.success(scheduleMapper.listByNurseAndDate(nurseId, java.time.LocalDate.parse(date)));
    }

    @GetMapping("/schedules/range")
    public ApiResponse<List<Schedule>> listSchedulesRange(@RequestParam String start, @RequestParam String end) {
        Long nurseId = Long.valueOf(StpUtil.getLoginId().toString());
        return ApiResponse.success(scheduleMapper.listByNurseAndDateRange(
            nurseId,
            java.time.LocalDate.parse(start),
            java.time.LocalDate.parse(end)
        ));
    }

    @PostMapping("/care-records")
    public ApiResponse<CareRecord> createCare(@RequestBody CareRecordRequest request) {
        CareRecord record = new CareRecord();
        record.setElderId(request.getElderId());
        record.setNurseId(Long.valueOf(StpUtil.getLoginId().toString()));
        record.setContent(request.getContent());
        record.setAttachmentUrl(request.getAttachmentUrl());
        record.setRecordTime(request.getRecordTime() == null ? java.time.LocalDateTime.now() : request.getRecordTime());
        careRecordMapper.insert(record);
        return ApiResponse.success(record);
    }

    @GetMapping("/care-records")
    public ApiResponse<List<CareRecord>> listCare(@RequestParam Long elderId) {
        return ApiResponse.success(careRecordMapper.listByElderId(elderId));
    }

    @PostMapping("/health-records")
    public ApiResponse<HealthRecord> createHealth(@RequestBody HealthRecordRequest request) {
        HealthRecord record = new HealthRecord();
        record.setElderId(request.getElderId());
        record.setNurseId(Long.valueOf(StpUtil.getLoginId().toString()));
        record.setTemperature(request.getTemperature());
        record.setBpSystolic(request.getBpSystolic());
        record.setBpDiastolic(request.getBpDiastolic());
        record.setHeartRate(request.getHeartRate());
        record.setNote(request.getNote());
        record.setRecordTime(request.getRecordTime() == null ? java.time.LocalDateTime.now() : request.getRecordTime());
        healthRecordMapper.insert(record);
        return ApiResponse.success(record);
    }

    @GetMapping("/health-records")
    public ApiResponse<List<HealthRecord>> listHealth(@RequestParam Long elderId) {
        return ApiResponse.success(healthRecordMapper.listByElderId(elderId));
    }

    @PostMapping("/handovers")
    public ApiResponse<Handover> createHandover(@RequestBody HandoverRequest request) {
        Handover handover = new Handover();
        handover.setNurseId(Long.valueOf(StpUtil.getLoginId().toString()));
        handover.setDate(request.getDate());
        handover.setContent(request.getContent());
        handoverMapper.insert(handover);
        return ApiResponse.success(handover);
    }

    @GetMapping("/handovers")
    public ApiResponse<List<Handover>> listHandovers() {
        Long nurseId = Long.valueOf(StpUtil.getLoginId().toString());
        return ApiResponse.success(handoverMapper.listByNurseId(nurseId));
    }

    @GetMapping("/notices")
    public ApiResponse<List<Notice>> listNotices() {
        Long nurseId = Long.valueOf(StpUtil.getLoginId().toString());
        return ApiResponse.success(noticeMapper.listByTarget("NURSE", nurseId));
    }
}
