using foodmarket.Application.Common.Tenancy;
using foodmarket.Application.Inventory;
using foodmarket.Domain.Inventory;
using foodmarket.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;

namespace foodmarket.Infrastructure.Inventory;

public class StockService : IStockService
{
    private readonly AppDbContext _db;
    private readonly ITenantContext _tenant;

    public StockService(AppDbContext db, ITenantContext tenant)
    {
        _db = db;
        _tenant = tenant;
    }

    public async Task<decimal> ApplyMovementAsync(StockMovementDraft d, CancellationToken ct = default)
    {
        var orgId = _tenant.OrganizationId ?? throw new InvalidOperationException("Tenant not set.");

        _db.StockMovements.Add(new StockMovement
        {
            OrganizationId = orgId,
            ProductId = d.ProductId,
            StoreId = d.StoreId,
            Quantity = d.Quantity,
            UnitCost = d.UnitCost,
            Type = d.Type,
            DocumentType = d.DocumentType,
            DocumentId = d.DocumentId,
            DocumentNumber = d.DocumentNumber,
            OccurredAt = d.OccurredAt ?? DateTime.UtcNow,
            CreatedByUserId = d.CreatedByUserId,
            Notes = d.Notes,
        });

        var stock = await _db.Stocks.FirstOrDefaultAsync(
            s => s.ProductId == d.ProductId && s.StoreId == d.StoreId, ct);

        if (stock is null)
        {
            stock = new Stock
            {
                OrganizationId = orgId,
                ProductId = d.ProductId,
                StoreId = d.StoreId,
                Quantity = d.Quantity,
            };
            _db.Stocks.Add(stock);
        }
        else
        {
            stock.Quantity += d.Quantity;
        }

        return stock.Quantity;
    }

    public async Task<decimal> ApplyMovementsAsync(IEnumerable<StockMovementDraft> drafts, CancellationToken ct = default)
    {
        var last = 0m;
        foreach (var d in drafts) last = await ApplyMovementAsync(d, ct);
        return last;
    }
}
