using PowderCoating.Core.Enums; // Still needed for MaintenanceStatus and MaintenancePriority

namespace PowderCoating.Application.DTOs.Dashboard;

public class SuperAdminDashboardViewModel
{
    // Platform-wide counts
    public int TotalCompanies { get; set; }
    public int ActiveCompanies { get; set; }
    public int InactiveCompanies { get; set; }
    public int TotalUsers { get; set; }

    // Subscription breakdown — DB-driven (keyed by plan int value)
    public Dictionary<int, (string DisplayName, int Count)> PlanDistribution { get; set; } = new();
    public int ActiveSubscriptions { get; set; }
    public int GracePeriodCount { get; set; }
    public int ExpiredCount { get; set; }

    // Companies needing attention (expired or grace period)
    public List<PlatformCompanyAlertDto> CompanyAlerts { get; set; } = new();

    // Recently registered companies
    public List<PlatformRecentCompanyDto> RecentCompanies { get; set; } = new();
}

public class PlatformCompanyAlertDto
{
    public int Id { get; set; }
    public string CompanyName { get; set; } = string.Empty;
    public int Plan { get; set; }
    public string PlanDisplayName { get; set; } = string.Empty;
    public SubscriptionStatus Status { get; set; }
    public DateTime? SubscriptionEndDate { get; set; }
    public int DaysOverdue { get; set; }
    public bool IsActive { get; set; }
}

public class PlatformRecentCompanyDto
{
    public int Id { get; set; }
    public string CompanyName { get; set; } = string.Empty;
    public int Plan { get; set; }
    public string PlanDisplayName { get; set; } = string.Empty;
    public SubscriptionStatus Status { get; set; }
    public bool IsActive { get; set; }
    public DateTime CreatedAt { get; set; }
}

public class DashboardViewModel
{
    // Summary counts
    public int ActiveJobsCount { get; set; }
    public int TodaysJobsCount { get; set; }
    public int OverdueJobsCount { get; set; }
    public int TodaysAppointmentsCount { get; set; }
    public int LowStockCount { get; set; }
    public int PendingMaintenanceCount { get; set; }
    public int PendingQuotesCount { get; set; }
    public decimal PendingQuoteValue { get; set; }
    public decimal MonthlyRevenue { get; set; }
    public int ActiveCustomersCount { get; set; }

    // Financial KPIs
    public decimal OutstandingAr { get; set; }
    public decimal CollectedThisMonth { get; set; }
    public decimal InvoicedThisMonth { get; set; }
    public int OverdueInvoicesCount { get; set; }
    public decimal OverdueInvoicesAmount { get; set; }

    // Tip of the day
    public string? TipOfTheDay { get; set; }

    // AR Aging buckets (balance due by days past due)
    public decimal AgingCurrent { get; set; }       // Not yet due
    public decimal AgingDays1To30 { get; set; }     // 1–30 days overdue
    public decimal AgingDays31To60 { get; set; }    // 31–60 days overdue
    public decimal AgingDays61To90 { get; set; }    // 61–90 days overdue
    public decimal AgingDaysOver90 { get; set; }    // 90+ days overdue

    // Bills Due
    public List<DashboardBillDto> BillsDue { get; set; } = new();
    public int BillsDueCount { get; set; }
    public decimal BillsDueAmount { get; set; }

    // Powder order tracking
    public List<PowderOrderVendorGroupDto> PowderOrdersNeeded { get; set; } = new();
    public int PowderOrdersNeededCount { get; set; }

    // Powder ordered / awaiting receipt
    public List<PowderOrderVendorGroupDto> PowderOrdersPlaced { get; set; } = new();
    public int PowderOrdersPlacedCount { get; set; }

    // Sections
    public List<DashboardJobDto> TodaysJobs { get; set; } = new();
    public List<DashboardAppointmentDto> TodaysAppointments { get; set; } = new();
    public List<DashboardJobDto> OverdueJobs { get; set; } = new();
    public List<DashboardQuoteDto> ExpiringQuotes { get; set; } = new();
    public List<DashboardJobDto> ActiveJobs { get; set; } = new();
    public List<DashboardLowStockDto> LowStockItems { get; set; } = new();
    public List<DashboardMaintenanceDto> UpcomingMaintenance { get; set; } = new();
    public List<DashboardEquipmentAlertDto> EquipmentAlerts { get; set; } = new();
    public List<DashboardQuoteDto> PendingQuotes { get; set; } = new();
    public List<DashboardRecentActivityDto> RecentActivity { get; set; } = new();
    public List<DashboardInvoiceDto> OverdueInvoices { get; set; } = new();
    public List<DashboardPaymentDto> RecentPayments { get; set; } = new();
}

public class DashboardBillDto
{
    public int Id { get; set; }
    public string BillNumber { get; set; } = string.Empty;
    public string VendorName { get; set; } = string.Empty;
    public decimal BalanceDue { get; set; }
    public DateTime? DueDate { get; set; }
    public bool IsOverdue { get; set; }
    public int DaysOverdue { get; set; }
}

public class DashboardInvoiceDto
{
    public int Id { get; set; }
    public string InvoiceNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public decimal Total { get; set; }
    public decimal BalanceDue { get; set; }
    public DateTime? DueDate { get; set; }
    public int DaysOverdue { get; set; }
}

public class DashboardPaymentDto
{
    public int Id { get; set; }
    public int InvoiceId { get; set; }
    public string InvoiceNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public decimal Amount { get; set; }
    public DateTime PaymentDate { get; set; }
    public string PaymentMethodDisplay { get; set; } = string.Empty;
}

public class DashboardJobDto
{
    public int Id { get; set; }
    public string JobNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public string StatusCode { get; set; } = string.Empty;
    public string StatusDisplayName { get; set; } = string.Empty;
    public string StatusColorClass { get; set; } = string.Empty;
    public string PriorityCode { get; set; } = string.Empty;
    public string PriorityDisplayName { get; set; } = string.Empty;
    public string PriorityColorClass { get; set; } = string.Empty;
    public DateTime? ScheduledDate { get; set; }
    public DateTime? DueDate { get; set; }
    public string? AssignedWorkerName { get; set; }
}

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

public class DashboardMaintenanceDto
{
    public int Id { get; set; }
    public string EquipmentName { get; set; } = string.Empty;
    public string MaintenanceType { get; set; } = string.Empty;
    public MaintenanceStatus Status { get; set; }
    public MaintenancePriority Priority { get; set; }
    public DateTime ScheduledDate { get; set; }
    public string Description { get; set; } = string.Empty;
    public string? AssignedWorkerName { get; set; }
}

public class DashboardQuoteDto
{
    public int Id { get; set; }
    public string QuoteNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public DateTime QuoteDate { get; set; }
    public DateTime? ExpirationDate { get; set; }
    public decimal Total { get; set; }
    public string StatusCode { get; set; } = string.Empty;
    public string StatusDisplayName { get; set; } = string.Empty;
}

public class DashboardAppointmentDto
{
    public int Id { get; set; }
    public string AppointmentNumber { get; set; } = string.Empty;
    public string Title { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public DateTime ScheduledStartTime { get; set; }
    public DateTime ScheduledEndTime { get; set; }
    public bool IsAllDay { get; set; }
    public string TypeDisplayName { get; set; } = string.Empty;
    public string TypeColorClass { get; set; } = string.Empty;
    public string StatusDisplayName { get; set; } = string.Empty;
    public string StatusColorClass { get; set; } = string.Empty;
    public string? AssignedWorkerName { get; set; }
}

public class DashboardEquipmentAlertDto
{
    public int Id { get; set; }
    public string EquipmentName { get; set; } = string.Empty;
    public string EquipmentType { get; set; } = string.Empty;
    public string Issue { get; set; } = string.Empty;
    public string Severity { get; set; } = string.Empty; // "Critical", "Warning", "Info"
    public DateTime? LastMaintenanceDate { get; set; }
    public DateTime? NextMaintenanceDue { get; set; }
}

public class PowderOrderVendorGroupDto
{
    public int? VendorId { get; set; }
    public string VendorName { get; set; } = "No Vendor Assigned";
    public string? VendorPhone { get; set; }
    public string? VendorEmail { get; set; }
    public decimal TotalLbsNeeded { get; set; }
    public decimal TotalEstCost { get; set; }
    public List<PowderOrderLineDto> Lines { get; set; } = new();
}

/// <summary>
/// One job's contribution to a merged powder order line.
/// </summary>
public class PowderOrderJobRefDto
{
    public int JobId { get; set; }
    public string JobNumber { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public decimal LbsToOrder { get; set; }
}

public class PowderOrderLineDto
{
    /// <summary>All coat IDs contributing to this line (&gt;1 when multiple jobs need the same powder).</summary>
    public List<int> CoatIds { get; set; } = new();

    /// <summary>Per-job breakdown; parallel to CoatIds.</summary>
    public List<PowderOrderJobRefDto> Jobs { get; set; } = new();

    // Convenience accessors for single-coat scenarios (the "placed" panel is always per-coat).
    public int CoatId => CoatIds.FirstOrDefault();
    public int JobId => Jobs.FirstOrDefault()?.JobId ?? 0;
    public string JobNumber => Jobs.FirstOrDefault()?.JobNumber ?? string.Empty;
    public string CustomerName => Jobs.FirstOrDefault()?.CustomerName ?? string.Empty;

    public string CoatName { get; set; } = string.Empty;
    public string? ColorName { get; set; }
    public string? ColorCode { get; set; }
    public string? Finish { get; set; }
    public string? SKU { get; set; }
    public decimal LbsToOrder { get; set; }
    public decimal? CostPerLb { get; set; }
    public decimal? EstCost => CostPerLb.HasValue ? LbsToOrder * CostPerLb.Value : null;
    public DateTime? OrderedAt { get; set; }
    public bool HasInventoryItem { get; set; }
    public int? VendorId { get; set; }
}

public class DashboardRecentActivityDto
{
    public int Id { get; set; }
    public string Type { get; set; } = string.Empty; // "Quote", "Job", "Customer"
    public string Title { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public string CustomerName { get; set; } = string.Empty;
    public DateTime ActivityDate { get; set; }
    public string? StatusDisplayName { get; set; }
    public string? StatusColorClass { get; set; }
    public decimal? Amount { get; set; }
}
