using System.ComponentModel.DataAnnotations;
using ERPCore.Domain.Enums;

namespace ERPCore.Dtos.Procurement;

// Responses (docs/11 §3.4) ------------------------------------------------------

public sealed record PurchaseReturnLineDto(int ReturnLineId, int? GrnLineId, int ItemId, decimal Qty);

public sealed record PurchaseReturnDto(
    int ReturnId, string DocNo, int VendorId, int WarehouseId, int ReasonCodeId, ReturnStatus Status,
    int CreatedBy, DateTime CreatedAt, string? GlJournalNo, DateTime? GlPostedAt,
    IReadOnlyList<PurchaseReturnLineDto> Lines, IReadOnlyList<int> LedgerRefs);

/// <summary>Row shape for <c>GET /purchase-returns</c> — no lines/ledgerRefs (those need a per-row query).</summary>
public sealed record PurchaseReturnSummaryDto(
    int ReturnId, string DocNo, int VendorId, int WarehouseId, int ReasonCodeId, ReturnStatus Status,
    int CreatedBy, DateTime CreatedAt, int LineCount, string? GlJournalNo);

// Requests ----------------------------------------------------------------------

public sealed class CreatePurchaseReturnLineInput
{
    /// <summary>Original GRN line, for traceability against the receipt.</summary>
    public int? GrnLineId { get; set; }
    [Required] public int ItemId { get; set; }
    [Range(0.0001, double.MaxValue)] public decimal Qty { get; set; }
}

public sealed class CreatePurchaseReturnRequest
{
    [Required] public int VendorId { get; set; }
    [Required] public int WarehouseId { get; set; }
    /// <summary>Nullable so an omitted value is a distinct <c>REASON_CODE_REQUIRED</c> error.</summary>
    public int? ReasonCodeId { get; set; }
    [Required, MinLength(1)] public List<CreatePurchaseReturnLineInput> Lines { get; set; } = new();
}
