namespace PowderCoating.Web.ViewModels.Reports;

// ── Shared item types used across analytics reports ─────────────────────────

public class TopCustomerItem
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public decimal Revenue { get; set; }
    public int JobCount { get; set; }
}

public class LowStockItem
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string? ColorName { get; set; }
    public decimal QuantityOnHand { get; set; }
    public decimal ReorderPoint { get; set; }
    public string UnitOfMeasure { get; set; } = string.Empty;
}

public class WorkerStatsItem
{
    public string Name { get; set; } = string.Empty;
    public string Role { get; set; } = string.Empty;
    public int JobsAssigned { get; set; }
    public int JobsCompleted { get; set; }
    public int AppointmentsAssigned { get; set; }
    public decimal CompletionRate => JobsAssigned > 0 ? Math.Round((decimal)JobsCompleted / JobsAssigned * 100, 1) : 0;
}

public class CustomerLifetimeValueItem
{
    public string? CustomerName { get; set; }
    public decimal TotalRevenue { get; set; }
    public int JobCount { get; set; }
    public decimal AvgOrderValue { get; set; }
    public DateTime FirstJobDate { get; set; }
    public DateTime LastJobDate { get; set; }
}

public class QuoteConversionFunnel
{
    public int Draft { get; set; }
    public int Sent { get; set; }
    public int Approved { get; set; }
    public int Converted { get; set; }
    public int Rejected { get; set; }
    public int Expired { get; set; }
    public int Total => Draft + Sent + Approved + Converted + Rejected + Expired;
    public decimal ConversionRate => Total > 0 ? Math.Round((decimal)(Approved + Converted) / Total * 100, 1) : 0;
}

public class AgingBucketItem
{
    public string Label { get; set; } = string.Empty;
    public decimal Amount { get; set; }
    public int Count { get; set; }
}

public class RecentPaymentItem
{
    public string InvoiceNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public decimal Amount { get; set; }
    public string PaymentMethod { get; set; } = string.Empty;
    public DateTime PaymentDate { get; set; }
}

public class OutstandingCustomerItem
{
    public string CustomerName { get; set; } = string.Empty;
    public decimal OutstandingBalance { get; set; }
    public int OpenInvoiceCount { get; set; }
}

public class VendorSpendItem
{
    public string VendorName { get; set; } = string.Empty;
    public decimal TotalBilled { get; set; }
    public decimal TotalPaid { get; set; }
    public int BillCount { get; set; }
    public decimal BalanceDue => TotalBilled - TotalPaid;
}

public class ExpenseByAccountItem
{
    public string AccountName { get; set; } = string.Empty;
    public decimal Amount { get; set; }
    public int Count { get; set; }
}

public class PowderUsageByColorItem
{
    public int InventoryItemId { get; set; }
    public string ColorName { get; set; } = string.Empty;
    public string? ColorCode { get; set; }
    public string? SKU { get; set; }
    public string? Manufacturer { get; set; }
    public decimal TotalLbsUsed { get; set; }
    public decimal TotalCost { get; set; }
    public int JobCount { get; set; }
    public string DisplayLabel => string.IsNullOrWhiteSpace(ColorCode)
        ? ColorName
        : $"{ColorName} ({ColorCode})";
}

public class SalesByCustomerItem
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; } = string.Empty;
    public bool IsCommercial { get; set; }
    public int InvoiceCount { get; set; }
    public decimal TotalInvoiced { get; set; }
    public decimal TotalPaid { get; set; }
    public decimal BalanceDue => TotalInvoiced - TotalPaid;
    public decimal AvgInvoiceValue => InvoiceCount > 0 ? Math.Round(TotalInvoiced / InvoiceCount, 2) : 0;
    public DateTime? LastInvoiceDate { get; set; }
}

public class CustomerRetentionItem
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; } = string.Empty;
    public string? Email { get; set; }
    public string? Phone { get; set; }
    public int TotalJobs { get; set; }
    public decimal LifetimeRevenue { get; set; }
    public DateTime? LastJobDate { get; set; }
    public int DaysSinceLastJob { get; set; }
    public string RetentionStatus { get; set; } = string.Empty;
}

public class JobCycleTimeItem
{
    public string StatusCode { get; set; } = string.Empty;
    public string StatusName { get; set; } = string.Empty;
    public int DisplayOrder { get; set; }
    public double AvgDays { get; set; }
    public double MinDays { get; set; }
    public double MaxDays { get; set; }
    public int JobCount { get; set; }
}

public class JobStatusAgingItem
{
    public int JobId { get; set; }
    public string JobNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public string StatusName { get; set; } = string.Empty;
    public string StatusCode { get; set; } = string.Empty;
    public string PriorityName { get; set; } = string.Empty;
    public string PriorityCode { get; set; } = string.Empty;
    public int DaysInCurrentStatus { get; set; }
    public DateTime? DueDate { get; set; }
    public bool IsOverdue { get; set; }
}

public class InvoiceAgingDetailItem
{
    public int InvoiceId { get; set; }
    public string InvoiceNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public string? CustomerEmail { get; set; }
    public string? CustomerPhone { get; set; }
    public DateTime InvoiceDate { get; set; }
    public DateTime? DueDate { get; set; }
    public decimal Total { get; set; }
    public decimal AmountPaid { get; set; }
    public decimal BalanceDue { get; set; }
    public int DaysOverdue { get; set; }
    public string AgingBucket { get; set; } = string.Empty;
    public string StatusDisplay { get; set; } = string.Empty;
}

public class PowderConsumptionItem
{
    public int InventoryItemId { get; set; }
    public string ItemName { get; set; } = string.Empty;
    public string? SKU { get; set; }
    public string? ColorName { get; set; }
    public string? ColorCode { get; set; }
    public string? Manufacturer { get; set; }
    public decimal TotalPurchasedLbs { get; set; }
    public decimal TotalConsumedLbs { get; set; }
    public decimal VarianceLbs => TotalPurchasedLbs - TotalConsumedLbs;
    public int PurchaseCount { get; set; }
    public int UsageJobCount { get; set; }
}

public class InventoryTurnoverItem
{
    public int InventoryItemId { get; set; }
    public string ItemName { get; set; } = string.Empty;
    public string? SKU { get; set; }
    public string? ColorName { get; set; }
    public decimal CurrentStockLbs { get; set; }
    public decimal TotalConsumedLbs { get; set; }
    public decimal TotalPurchasedLbs { get; set; }
    public decimal DailyConsumptionLbs { get; set; }
    public double DaysToStockout { get; set; }
    public double TurnoverRate { get; set; }
    public string StockStatus { get; set; } = string.Empty;
}
