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

namespace ERPCore.Dtos.Sales;

public sealed record BundleSaleLineDto(
    int BundleSaleLineId, int ItemId, string Description, decimal Qty, int WarehouseId,
    decimal UnitPrice, decimal LineTotal, bool IncludeInBundle, bool IsComponent, int? ParentLineId);

public sealed record BundleSaleDto(
    int BundleSaleId, string BundleNo, DateTime BundleDate, int CustomerId, string CustomerSnapshotName,
    int WarehouseId, int CashierUserId, int BundleSaleTemplateId, string BundleName, string BundleCode,
    BundleSaleStatus Status, decimal ComponentSubtotal, decimal BundlePrice, decimal MarginAmount,
    decimal DiscountTotal, decimal TaxTotal, decimal GrandTotal, DateTime CreatedAt, DateTime? UpdatedAt,
    IReadOnlyList<BundleSaleLineDto> Lines);

public sealed record BundleSaleSummaryDto(
    int BundleSaleId, string BundleNo, DateTime BundleDate, int CustomerId, string CustomerSnapshotName,
    int WarehouseId, string BundleName, string BundleCode, BundleSaleStatus Status,
    decimal ComponentSubtotal, decimal BundlePrice, decimal GrandTotal, DateTime CreatedAt);

public sealed record BundleSaleTemplateLineDto(
    int BundleSaleTemplateLineId, int ItemId, int WarehouseId, decimal Qty,
    decimal UnitPrice, bool IncludeInBundle, int SortOrder);

public sealed record BundleSaleTemplateDto(
    int BundleSaleTemplateId, string TemplateCode, string TemplateName, string? Description,
    EntityStatus Status, DateTime CreatedAt, DateTime? UpdatedAt, IReadOnlyList<BundleSaleTemplateLineDto> Lines);

public sealed record BundleSaleTemplateSummaryDto(
    int BundleSaleTemplateId, string TemplateCode, string TemplateName, string? Description,
    EntityStatus Status, int LineCount, DateTime CreatedAt, DateTime? UpdatedAt);

public sealed record BundleSalePostingIssueDto(
    int BundleSaleLineId, int ItemId, string ItemSku, string ItemName, int WarehouseId,
    decimal RequestedQty, decimal AvailableQty, decimal ShortQty);

public sealed record BundleSalePostingCheckDto(
    int BundleSaleId, string BundleNo, BundleSaleStatus Status, bool CanPost,
    IReadOnlyList<BundleSalePostingIssueDto> Issues);

public sealed class CreateBundleSaleTemplateLineRequest
{
    [Required] public int ItemId { get; set; }
    [Required] public int WarehouseId { get; set; }
    [Range(0.0001, double.MaxValue)] public decimal Qty { get; set; }
    [Range(0, double.MaxValue)] public decimal UnitPrice { get; set; }
    public bool IncludeInBundle { get; set; } = true;
    public int SortOrder { get; set; }
}

public sealed class CreateBundleSaleTemplateRequest
{
    [Required] public string TemplateCode { get; set; } = string.Empty;
    [Required] public string TemplateName { get; set; } = string.Empty;
    public string? Description { get; set; }
    [Required, MinLength(1)] public List<CreateBundleSaleTemplateLineRequest> Lines { get; set; } = new();
}

public sealed class CreateBundleSaleRequest
{
    [Required] public int CustomerId { get; set; }
    [Required] public int WarehouseId { get; set; }
    [Required] public int CashierUserId { get; set; }
    [Required] public int BundleSaleTemplateId { get; set; }
    [Required] public string BundleName { get; set; } = string.Empty;
    [Range(0, double.MaxValue)] public decimal BundlePrice { get; set; }
    public bool AllowPriceOverride { get; set; }
    [Required, MinLength(1)] public List<CreateBundleSaleTemplateLineRequest> Lines { get; set; } = new();
}

public sealed class UpdateBundleSaleRequest
{
    [Required] public int CustomerId { get; set; }
    [Required] public int WarehouseId { get; set; }
    [Required] public int CashierUserId { get; set; }
    [Required] public int BundleSaleTemplateId { get; set; }
    [Required] public string BundleName { get; set; } = string.Empty;
    [Range(0, double.MaxValue)] public decimal BundlePrice { get; set; }
    public bool AllowPriceOverride { get; set; }
    [Required, MinLength(1)] public List<CreateBundleSaleTemplateLineRequest> Lines { get; set; } = new();
}
