package com.petshop.backend.controller;

import com.petshop.backend.dto.productsupplier.BulkDeleteProductSupplierRequest;
import com.petshop.backend.dto.productsupplier.ProductSupplierRequest;
import com.petshop.backend.dto.productsupplier.ProductSupplierResponse;
import com.petshop.backend.service.ProductSupplierService;
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/product-suppliers")
@PreAuthorize("hasRole('ADMIN')")
public class ProductSupplierController {

    private final ProductSupplierService productSupplierService;

    public ProductSupplierController(ProductSupplierService productSupplierService) {
        this.productSupplierService = productSupplierService;
    }

    @GetMapping
    public ResponseEntity<Page<ProductSupplierResponse>> getAllProductSuppliers(
            @RequestParam(required = false) String q,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) Long supplierId,
            Pageable pageable) {
        return ResponseEntity.ok(productSupplierService.getAllProductSuppliers(q, productId, supplierId, pageable));
    }

    @GetMapping("/{productId}/{supplierId}")
    public ResponseEntity<ProductSupplierResponse> getProductSupplierById(
            @PathVariable Long productId,
            @PathVariable Long supplierId) {
        return ResponseEntity.ok(productSupplierService.getProductSupplierById(productId, supplierId));
    }

    @PostMapping
    public ResponseEntity<ProductSupplierResponse> createProductSupplier(@Valid @RequestBody ProductSupplierRequest request) {
        return ResponseEntity.status(HttpStatus.CREATED).body(productSupplierService.createProductSupplier(request));
    }

    @PutMapping("/{productId}/{supplierId}")
    public ResponseEntity<ProductSupplierResponse> updateProductSupplier(
            @PathVariable Long productId,
            @PathVariable Long supplierId,
            @Valid @RequestBody ProductSupplierRequest request) {
        return ResponseEntity.ok(productSupplierService.updateProductSupplier(productId, supplierId, request));
    }

    @DeleteMapping("/{productId}/{supplierId}")
    public ResponseEntity<Void> deleteProductSupplier(
            @PathVariable Long productId,
            @PathVariable Long supplierId) {
        productSupplierService.deleteProductSupplier(productId, supplierId);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping
    public ResponseEntity<Void> bulkDeleteProductSuppliers(@Valid @RequestBody BulkDeleteProductSupplierRequest request) {
        productSupplierService.bulkDeleteProductSuppliers(request);
        return ResponseEntity.noContent().build();
    }
}
