package com.gpf.pethospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gpf.pethospital.common.ApiResponse;
import com.gpf.pethospital.entity.Appointment;
import com.gpf.pethospital.security.AuthUser;
import com.gpf.pethospital.service.AppointmentService;
import com.gpf.pethospital.util.SecurityUtils;
// import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/appointments")
public class AppointmentController {
    private final AppointmentService appointmentService;

    public AppointmentController(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }

    @PostMapping
    public ApiResponse<?> create(@RequestBody Appointment appointment) {
        AuthUser user = SecurityUtils.currentUser();
        if (user != null && "CUSTOMER".equals(user.getRole())) {
            appointment.setCustomerId(user.getId());
        } else if (appointment.getCustomerId() == null) {
            return ApiResponse.error(400, "customerId required");
        }
        if (appointment.getStatus() == null) {
            appointment.setStatus("PENDING");
        }
        appointmentService.save(appointment);
        return ApiResponse.success("created", null);
    }

    @GetMapping
    public ApiResponse<?> list(@RequestParam(defaultValue = "1") long page,
                               @RequestParam(defaultValue = "10") long size) {
        AuthUser user = SecurityUtils.currentUser();
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<>();
        if (user != null && "CUSTOMER".equals(user.getRole())) {
            wrapper.eq(Appointment::getCustomerId, user.getId());
        }
        return ApiResponse.success(appointmentService.page(new Page<>(page, size), wrapper));
    }

    // @PreAuthorize("hasAnyRole('ADMIN','DOCTOR')")
    @GetMapping("/admin")
    public ApiResponse<?> adminList(@RequestParam(defaultValue = "1") long page,
                                    @RequestParam(defaultValue = "10") long size,
                                    @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<>();
        if (status != null && !status.isBlank()) {
            wrapper.eq(Appointment::getStatus, status);
        }
        return ApiResponse.success(appointmentService.page(new Page<>(page, size), wrapper));
    }

    // @PreAuthorize("hasAnyRole('ADMIN','DOCTOR')")
    @PutMapping("/{id}/status")
    public ApiResponse<?> updateStatus(@PathVariable Long id, @RequestParam String status) {
        Appointment update = new Appointment();
        update.setId(id);
        update.setStatus(status);
        if ("CANCELLED".equals(status)) {
            update.setCancelTime(LocalDateTime.now());
        }
        appointmentService.updateById(update);
        return ApiResponse.success("updated", null);
    }
}
