package com.platform2easy.genesis.web.controller;

import com.platform2easy.genesis.domain.model.Cpr;
import com.platform2easy.genesis.domain.service.CprService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cpr")
@AllArgsConstructor
public class CprController {

    private final CprService service;

    // GET /api/cpr
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<Cpr> listAll() {
        return service.listarTodos();
    }

    // GET /api/cpr/{id}
    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Cpr getById(@PathVariable Long id) {
        return service.buscarPorId(id);
    }

    // POST /api/cpr
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Cpr create(@RequestBody Cpr cpr) {
        cpr.setId(null);
        return service.salvar(cpr);
    }

    // PUT /api/cpr/{id}
    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Cpr update(@PathVariable Long id, @RequestBody Cpr cpr) {
        cpr.setId(id);
        return service.salvar(cpr);
    }

    // DELETE /api/cpr/{id}
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) {
        service.deletarPorId(id);
    }
}