using CashLineERP.Domain.Common;
using CashLineERP.SharedKernel.Workflow;

namespace CashLineERP.Domain.Sales;

public sealed class SalesReturn : Entity
{
    public long Id { get; private set; }

    public string SalesReturnNumber { get; private set; } = string.Empty;

    public long BranchId { get; private set; }

    public long CustomerId { get; private set; }

    public long SalesOrderId { get; private set; }

    public long DispatchOrderId { get; private set; }

    public DateOnly ReturnDate { get; private set; }

    public long ReturnWarehouseId { get; private set; }

    public string ReasonCode { get; private set; } = string.Empty;

    public string? Notes { get; private set; }

    public long StatusId { get; private set; } = WorkflowStatusSeedIds.Draft;

    public decimal TotalReturnedQuantity { get; private set; }

    public long? CreditNoteInvoiceId { get; private set; }

    public DateTime? CreditNoteIssuedAtUtc { get; private set; }

    public List<SalesReturnLine> Lines { get; private set; } = [];

    public SalesReturn(
        string salesReturnNumber,
        long branchId,
        long customerId,
        long salesOrderId,
        long dispatchOrderId,
        DateOnly returnDate,
        long returnWarehouseId,
        string reasonCode,
        string? notes)
    {
        SalesReturnNumber = salesReturnNumber;
        BranchId = branchId;
        CustomerId = customerId;
        SalesOrderId = salesOrderId;
        DispatchOrderId = dispatchOrderId;
        ReturnDate = returnDate;
        ReturnWarehouseId = returnWarehouseId;
        ReasonCode = reasonCode;
        Notes = notes;
    }

    private SalesReturn()
    {
    }

    public void AddLine(
        int lineNumber,
        long dispatchOrderLineId,
        long salesOrderLineId,
        long itemId,
        decimal quantityReturned,
        long? warehouseLocationId,
        string? batchNumber,
        string? serialNumber,
        string? notes)
    {
        Lines.Add(new SalesReturnLine(
            salesReturnId: Id,
            lineNumber: lineNumber,
            dispatchOrderLineId: dispatchOrderLineId,
            salesOrderLineId: salesOrderLineId,
            itemId: itemId,
            quantityReturned: quantityReturned,
            warehouseLocationId: warehouseLocationId,
            batchNumber: batchNumber,
            serialNumber: serialNumber,
            notes: notes));

        RecalculateTotals();
    }

    public void Submit(long actorUserId)
    {
        StatusId = WorkflowStatusSeedIds.Submitted;
        Touch(actorUserId);
    }

    public void Post(long actorUserId)
    {
        StatusId = WorkflowStatusSeedIds.Posted;
        Touch(actorUserId);
    }

    public void LinkCreditNote(long actorUserId, long creditNoteInvoiceId)
    {
        CreditNoteInvoiceId = creditNoteInvoiceId;
        CreditNoteIssuedAtUtc = DateTime.UtcNow;
        Touch(actorUserId);
    }

    private void RecalculateTotals()
    {
        TotalReturnedQuantity = Lines.Sum(line => line.QuantityReturned);
    }
}
