package pl.polskalokalnie.campaign;

import jakarta.validation.Valid;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * Kampanie marketingowe w panelu administratora.
 */
@RestController
@RequestMapping("/api/admin/campaigns")
public class CampaignController {

    private final CampaignService campaignService;

    public CampaignController(CampaignService campaignService) {
        this.campaignService = campaignService;
    }

    @GetMapping
    public List<CampaignResponse> list() {
        return campaignService.list();
    }

    @GetMapping("/{id}")
    public CampaignResponse get(@PathVariable Long id) {
        return campaignService.get(id);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public CampaignResponse create(@Valid @RequestBody CampaignRequest request, Authentication authentication) {
        return campaignService.create(request, authentication.getName());
    }

    @PutMapping("/{id}")
    public CampaignResponse update(@PathVariable Long id, @Valid @RequestBody CampaignRequest request) {
        return campaignService.update(id, request);
    }

    @PostMapping("/{id}/schedule")
    public CampaignResponse schedule(@PathVariable Long id) {
        return campaignService.schedule(id);
    }

    @PostMapping("/{id}/pause")
    public CampaignResponse pause(@PathVariable Long id) {
        return campaignService.pause(id);
    }

    @GetMapping("/{id}/stats")
    public CampaignStatsResponse stats(@PathVariable Long id) {
        return campaignService.stats(id);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        campaignService.delete(id);
        return ResponseEntity.noContent().build();
    }
}
