package com.pressing.modules.sites;

import com.pressing.modules.sites.dto.SiteDtos.SiteRequest;
import com.pressing.modules.sites.dto.SiteDtos.SiteResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    private final SiteService siteService;

    public SiteController(SiteService siteService) {
        this.siteService = siteService;
    }

    @GetMapping
    public List<SiteResponse> sites(@RequestParam(required = false) String q) {
        return siteService.sites(q);
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public SiteResponse createSite(@Valid @RequestBody SiteRequest req) {
        return siteService.createSite(req);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public SiteResponse updateSite(@PathVariable Long id, @Valid @RequestBody SiteRequest req) {
        return siteService.updateSite(id, req);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteSite(@PathVariable Long id) {
        siteService.deleteSite(id);
    }
}