package com.gpf.pethospital.controller;

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

@RestController
@RequestMapping("/prescription-items")
public class PrescriptionItemController {
    private final PrescriptionItemService prescriptionItemService;

    public PrescriptionItemController(PrescriptionItemService prescriptionItemService) {
        this.prescriptionItemService = prescriptionItemService;
    }

    @GetMapping
    public ApiResponse<?> list(@RequestParam Long prescriptionId) {
        LambdaQueryWrapper<PrescriptionItem> wrapper = new LambdaQueryWrapper<PrescriptionItem>()
                .eq(PrescriptionItem::getPrescriptionId, prescriptionId);
        return ApiResponse.success(prescriptionItemService.list(wrapper));
    }

    // @PreAuthorize("hasAnyRole('ADMIN','DOCTOR')")
    @PostMapping
    public ApiResponse<?> create(@RequestBody PrescriptionItem item) {
        prescriptionItemService.save(item);
        return ApiResponse.success("created", null);
    }

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

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