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

namespace ERPCore.Dtos.Items;

// Response DTOs (docs/11-BACKEND-PHASE1.md §2.1) --------------------------------

/// <summary>Row shape for <c>GET /items</c>.</summary>
public sealed record ItemListItemDto(
    int ItemId, string Sku, string Name, int CategoryId, int BaseUomId,
    int? DefaultVendorId, ItemType ItemType, TrackingMode TrackingMode,
    string? TaxClass, EntityStatus Status);

/// <summary>A single per-warehouse reorder policy row.</summary>
public sealed record ItemReorderDto(int WarehouseId, decimal ReorderPoint, decimal ReorderQty);

/// <summary>Full item resource for <c>GET /items/{id}</c> and create/update responses.</summary>
public sealed record ItemDetailDto(
    int ItemId, string Sku, string Name, string? Description, int CategoryId,
    int BaseUomId, int? DefaultVendorId, ItemType ItemType, TrackingMode TrackingMode,
    string? TaxClass, EntityStatus Status, IReadOnlyList<ItemReorderDto> Reorder,
    DateTime CreatedAt, DateTime? UpdatedAt);

/// <summary>UOM conversion row (docs/11 §2.2).</summary>
public sealed record UomConversionDto(int ConversionId, int FromUom, int ToUom, decimal Factor);

/// <summary>Response body for <c>PUT /items/{id}/uom-conversions</c>.</summary>
public sealed record ItemUomConversionsDto(int ItemId, int BaseUomId, IReadOnlyList<UomConversionDto> Conversions);

/// <summary>Response body for <c>PUT /items/{id}/reorder</c>.</summary>
public sealed record ItemReorderSettingsDto(IReadOnlyList<ItemReorderDto> Settings);

// Request DTOs — narrow: server-controlled fields (status, ids, timestamps)
// are intentionally excluded to prevent over-posting (02-SECURITY B.6 / C.1). ----

public sealed class CreateItemRequest
{
    [Required, StringLength(50)] public string Sku { get; set; } = string.Empty;
    [Required, StringLength(200)] public string Name { get; set; } = string.Empty;
    [StringLength(1000)] public string? Description { get; set; }
    [Required] public int CategoryId { get; set; }
    [Required] public int BaseUomId { get; set; }
    public int? DefaultVendorId { get; set; }
    [Required, EnumDataType(typeof(ItemType))] public ItemType ItemType { get; set; }
    [EnumDataType(typeof(TrackingMode))] public TrackingMode TrackingMode { get; set; } = TrackingMode.None;
    [StringLength(20)] public string? TaxClass { get; set; }
}

public sealed class UpdateItemRequest
{
    [Required, StringLength(50)] public string Sku { get; set; } = string.Empty;
    [Required, StringLength(200)] public string Name { get; set; } = string.Empty;
    [StringLength(1000)] public string? Description { get; set; }
    [Required] public int CategoryId { get; set; }
    [Required] public int BaseUomId { get; set; }
    public int? DefaultVendorId { get; set; }
    [Required, EnumDataType(typeof(ItemType))] public ItemType ItemType { get; set; }
    [EnumDataType(typeof(TrackingMode))] public TrackingMode TrackingMode { get; set; } = TrackingMode.None;
    [StringLength(20)] public string? TaxClass { get; set; }
}

public sealed class UpdateItemStatusRequest
{
    [Required, EnumDataType(typeof(EntityStatus))] public EntityStatus Status { get; set; }
}

public sealed class ReorderSettingInput
{
    [Required] public int WarehouseId { get; set; }
    [Range(0, double.MaxValue)] public decimal ReorderPoint { get; set; }
    [Range(0, double.MaxValue)] public decimal ReorderQty { get; set; }
}

public sealed class UpdateReorderRequest
{
    [Required, MinLength(1)] public List<ReorderSettingInput> Settings { get; set; } = new();
}

public sealed class UomConversionInput
{
    [Required] public int FromUom { get; set; }
    [Required] public int ToUom { get; set; }
    [Range(0.000001, double.MaxValue)] public decimal Factor { get; set; }
}

public sealed class UpdateUomConversionsRequest
{
    [Required, MinLength(1)] public List<UomConversionInput> Conversions { get; set; } = new();
}
