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.Pet;
import com.gpf.pethospital.security.AuthUser;
import com.gpf.pethospital.service.PetService;
import com.gpf.pethospital.util.SecurityUtils;
// import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pets")
public class PetController {
    private final PetService petService;

    public PetController(PetService petService) {
        this.petService = petService;
    }

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

    @PostMapping
    public ApiResponse<?> create(@RequestBody Pet pet) {
        AuthUser user = SecurityUtils.currentUser();
        if (user != null && "CUSTOMER".equals(user.getRole())) {
            pet.setOwnerId(user.getId());
        } else if (pet.getOwnerId() == null) {
            return ApiResponse.error(400, "ownerId required");
        }
        petService.save(pet);
        return ApiResponse.success("created", null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody Pet pet) {
        AuthUser user = SecurityUtils.currentUser();
        if (user != null && "CUSTOMER".equals(user.getRole())) {
            Pet existing = petService.getById(id);
            if (existing == null || !existing.getOwnerId().equals(user.getId())) {
                return ApiResponse.error(403, "forbidden");
            }
        }
        pet.setId(id);
        petService.updateById(pet);
        return ApiResponse.success("updated", null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        AuthUser user = SecurityUtils.currentUser();
        if (user != null && "CUSTOMER".equals(user.getRole())) {
            Pet existing = petService.getById(id);
            if (existing == null || !existing.getOwnerId().equals(user.getId())) {
                return ApiResponse.error(403, "forbidden");
            }
        }
        petService.removeById(id);
        return ApiResponse.success("deleted", null);
    }

    // @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin/all")
    public ApiResponse<?> adminList(@RequestParam(defaultValue = "1") long page,
                                    @RequestParam(defaultValue = "10") long size) {
        return ApiResponse.success(petService.page(new Page<>(page, size)));
    }
}
