using Inventario.Core.Interfaces;
using Inventario.Core.Models;
using Microsoft.AspNetCore.Mvc;

namespace Inventario.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class InventoryMovementsController : ControllerBase
{
    private readonly IInventoryMovementRepository _repository;

    public InventoryMovementsController(IInventoryMovementRepository repository)
    {
        _repository = repository;
    }

    [HttpGet]
    public async Task<ActionResult<IEnumerable<InventoryMovement>>> GetAll()
    {
        var movements = await _repository.GetAllAsync();
        return Ok(movements);
    }

    [HttpGet("product/{productId}")]
    public async Task<ActionResult<IEnumerable<InventoryMovement>>> GetByProduct(int productId)
    {
        var movements = await _repository.GetByProductIdAsync(productId);
        return Ok(movements);
    }

    [HttpPost]
    public async Task<ActionResult<InventoryMovement>> Create(InventoryMovement movement)
    {
        var created = await _repository.AddAsync(movement);
        return CreatedAtAction(nameof(GetByProduct), new { productId = created.ProductId }, created);
    }
}
