package com.platform2easy.genesis.web.controller;

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

import java.util.List;

@RestController
@RequestMapping("/api/commodity")
@AllArgsConstructor
public class CommodityController {

    private final CommodityService service; // Injeção de dependência mantida

    // GET /api/commodity: Lista todos
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<Commoditiy> listAll() {
        return service.listarTodos(); // Chamada ao método Service original
    }

    // GET /api/commodity/{id}: Busca por ID
    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Commoditiy getById(@PathVariable Long id) {
        return service.buscarPorId(id); // Chamada ao método Service original
    }

    // POST /api/commodity: Cria um novo recurso
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED) // Retorna 201 Created
    public Commoditiy create(@RequestBody Commoditiy commodity) {
        commodity.setId(null);
        return service.salvar(commodity);
    }

    // PUT /api/commodity/{id}: Atualiza um recurso existente
    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Commoditiy update(@PathVariable Long id, @RequestBody Commoditiy commodity) {
        commodity.setId(id);
        return service.salvar(commodity);
    }

    // DELETE /api/commodity/{id}: Deleta um recurso
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) {
        service.deletarPorId(id);
    }
}