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.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * Zarzadzanie baza leadow z panelu administratora. Ochrona przez regule /api/admin/** w
 * SecurityConfig (rola ADMIN).
 */
@RestController
@RequestMapping("/api/admin/leads")
public class LeadController {

    private final LeadService leadService;

    public LeadController(LeadService leadService) {
        this.leadService = leadService;
    }

    @GetMapping
    public List<LeadResponse> list(
            @RequestParam(required = false) LeadSource source,
            @RequestParam(required = false) String search
    ) {
        return leadService.list(source, search);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public LeadResponse create(@Valid @RequestBody LeadCreateRequest request) {
        return leadService.create(request);
    }

    @PostMapping("/import")
    public ImportResultResponse importCsv(@Valid @RequestBody LeadImportRequest request) {
        return leadService.importCsv(request);
    }

    @PatchMapping("/{id}")
    public LeadResponse update(@PathVariable Long id, @Valid @RequestBody LeadUpdateRequest request) {
        return leadService.update(id, request);
    }

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