package com.petshop.backend.controller;

import com.petshop.backend.dto.user.UserRequest;
import com.petshop.backend.dto.user.UserResponse;
import com.petshop.backend.entity.User;
import com.petshop.backend.service.UserService;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/employees")
@PreAuthorize("hasRole('ADMIN')")
public class EmployeeController {

    private final UserService userService;

    public EmployeeController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    public ResponseEntity<Page<UserResponse>> getAllEmployees(
            @RequestParam(required = false) String q,
            Pageable pageable) {
        return ResponseEntity.ok(userService.getAllUsers(q, "STAFF", pageable));
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserResponse> getEmployeeById(@PathVariable Long id) {
        return ResponseEntity.ok(userService.getUserById(id, User.Role.STAFF));
    }

    @PostMapping
    public ResponseEntity<UserResponse> createEmployee(@Valid @RequestBody UserRequest request) {
        return ResponseEntity.status(HttpStatus.CREATED).body(userService.createUser(request, User.Role.STAFF));
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserResponse> updateEmployee(
            @PathVariable Long id,
            @Valid @RequestBody UserRequest request) {
        return ResponseEntity.ok(userService.updateUser(id, request, User.Role.STAFF));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
        userService.deleteUser(id, User.Role.STAFF);
        return ResponseEntity.noContent().build();
    }
}
