package pl.polskalokalnie.lead;

import jakarta.validation.Valid;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

/**
 * Grupy docelowe (dynamiczne segmenty) w panelu administratora.
 */
@RestController
@RequestMapping("/api/admin/target-groups")
public class TargetGroupController {

    private final TargetGroupService targetGroupService;

    public TargetGroupController(TargetGroupService targetGroupService) {
        this.targetGroupService = targetGroupService;
    }

    @GetMapping
    public List<TargetGroupResponse> list() {
        return targetGroupService.list();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public TargetGroupResponse create(@Valid @RequestBody TargetGroupRequest request) {
        return targetGroupService.create(request);
    }

    @PutMapping("/{id}")
    public TargetGroupResponse update(@PathVariable Long id, @Valid @RequestBody TargetGroupRequest request) {
        return targetGroupService.update(id, request);
    }

    @GetMapping("/{id}/preview")
    public TargetGroupPreviewResponse preview(@PathVariable Long id) {
        return targetGroupService.preview(id);
    }

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