package com.gpf.pethospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gpf.pethospital.common.ApiResponse;
import com.gpf.pethospital.entity.MedicalRecord;
import com.gpf.pethospital.service.MedicalRecordService;
// import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/medical-records")
public class MedicalRecordController {
    private final MedicalRecordService medicalRecordService;

    public MedicalRecordController(MedicalRecordService medicalRecordService) {
        this.medicalRecordService = medicalRecordService;
    }

    // @PreAuthorize("hasAnyRole('ADMIN','DOCTOR')")
    @PostMapping
    public ApiResponse<?> create(@RequestBody MedicalRecord record) {
        if (record.getStatus() == null) {
            record.setStatus("DRAFT");
        }
        medicalRecordService.save(record);
        return ApiResponse.success("created", null);
    }

    @GetMapping
    public ApiResponse<?> list(@RequestParam Long visitId) {
        LambdaQueryWrapper<MedicalRecord> wrapper = new LambdaQueryWrapper<MedicalRecord>()
                .eq(MedicalRecord::getVisitId, visitId);
        return ApiResponse.success(medicalRecordService.list(wrapper));
    }

    // @PreAuthorize("hasAnyRole('ADMIN','DOCTOR')")
    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody MedicalRecord record) {
        record.setId(id);
        medicalRecordService.updateById(record);
        return ApiResponse.success("updated", null);
    }

    // @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        medicalRecordService.removeById(id);
        return ApiResponse.success("deleted", null);
    }
}
