package com.pressing.modules.catalog;

import com.pressing.modules.catalog.dto.CatalogDtos.ArticleRequest;
import com.pressing.modules.catalog.dto.CatalogDtos.ArticleTypeRequest;
import com.pressing.modules.catalog.dto.CatalogDtos.ColorRequest;
import com.pressing.modules.catalog.dto.CatalogDtos.LocationRequest;
import com.pressing.modules.catalog.dto.CatalogDtos.PricingRequest;
import com.pressing.modules.catalog.dto.CatalogDtos.ServiceRequest;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class CatalogController {

    private final CatalogService catalogService;

    public CatalogController(CatalogService catalogService) {
        this.catalogService = catalogService;
    }


    @GetMapping("/article-types")
    public Object types() {
        return catalogService.articleTypes();
    }

    @PostMapping("/article-types")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object createType(@Valid @RequestBody ArticleTypeRequest req) {
        return catalogService.createType(req);
    }

    @PutMapping("/article-types/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object updateType(@PathVariable Long id, @Valid @RequestBody ArticleTypeRequest req) {
        return catalogService.updateType(id, req);
    }

    @DeleteMapping("/article-types/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteType(@PathVariable Long id) {
        catalogService.deleteType(id);
    }

    @GetMapping("/articles")
    public Object articles(@RequestParam(required = false) Long typeId) {
        return catalogService.articles(typeId);
    }

    @PostMapping("/articles")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object createArticle(@Valid @RequestBody ArticleRequest req) {
        return catalogService.createArticle(req);
    }

    @PutMapping("/articles/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object updateArticle(@PathVariable Long id, @Valid @RequestBody ArticleRequest req) {
        return catalogService.updateArticle(id, req);
    }

    @DeleteMapping("/articles/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteArticle(@PathVariable Long id) {
        catalogService.deleteArticle(id);
    }

    @GetMapping("/colors")
    public Object colors() {
        return catalogService.colors();
    }

    @PostMapping("/colors")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object createColor(@Valid @RequestBody ColorRequest req) {
        return catalogService.createColor(req);
    }

    @PutMapping("/colors/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object updateColor(@PathVariable Long id, @Valid @RequestBody ColorRequest req) {
        return catalogService.updateColor(id, req);
    }

    @DeleteMapping("/colors/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteColor(@PathVariable Long id) {
        catalogService.deleteColor(id);
    }

    @GetMapping("/storage-locations")
    public Object locations() {
        return catalogService.locations();
    }

    @PostMapping("/storage-locations")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER','OPERATOR')")
    public Object createLocation(@Valid @RequestBody LocationRequest req) {
        return catalogService.createLocation(req);
    }

    @PutMapping("/storage-locations/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER','OPERATOR')")
    public Object updateLocation(@PathVariable Long id, @Valid @RequestBody LocationRequest req) {
        return catalogService.updateLocation(id, req);
    }

    @DeleteMapping("/storage-locations/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteLocation(@PathVariable Long id) {
        catalogService.deleteLocation(id);
    }

    @GetMapping("/services")
    public Object services() {
        return catalogService.services();
    }

    @PostMapping("/services")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object createService(@Valid @RequestBody ServiceRequest req) {
        return catalogService.createService(req);
    }

    @PutMapping("/services/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object updateService(@PathVariable Long id, @Valid @RequestBody ServiceRequest req) {
        return catalogService.updateService(id, req);
    }

    @DeleteMapping("/services/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteService(@PathVariable Long id) {
        catalogService.deleteService(id);
    }

    @GetMapping("/pricings")
    public Object pricings() {
        return catalogService.pricings();
    }

    @PostMapping("/pricings")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object createPricing(@Valid @RequestBody PricingRequest req) {
        return catalogService.createPricing(req);
    }

    @PutMapping("/pricings/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','MANAGER')")
    public Object updatePricing(@PathVariable Long id, @Valid @RequestBody PricingRequest req) {
        return catalogService.updatePricing(id, req);
    }

    @DeleteMapping("/pricings/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deletePricing(@PathVariable Long id) {
        catalogService.deletePricing(id);
    }

    @GetMapping("/pricings/lookup")
    public Object lookupPricing(
            @RequestParam Long serviceId,
            @RequestParam Long articleTypeId) {
        return catalogService.getPricingForServiceAndType(serviceId, articleTypeId);
    }
}
