import 'package:equatable/equatable.dart';
import '../../domain/models/billing_period.dart';
import '../models/billing_invoice_model.dart';
import '../models/spending_breakdown_model.dart';

/// The loading status of the billing feature.
enum BillingStatus {
  /// Page hasn't started loading.
  initial,

  /// Data is currently being fetched.
  loading,

  /// Data loaded successfully.
  success,

  /// Loading failed.
  failure,
}

/// Represents the state of the billing feature.
class BillingState extends Equatable {
  /// Creates a [BillingState].
  const BillingState({
    this.status = BillingStatus.initial,
    this.currentBill = 0.0,
    this.savings = 0.0,
    this.pendingInvoices = const <BillingInvoice>[],
    this.invoiceHistory = const <BillingInvoice>[],
    this.spendingBreakdown = const <SpendingBreakdownItem>[],
    this.period = BillingPeriod.week,
    this.errorMessage,
  });

  /// The current feature status.
  final BillingStatus status;

  /// The total amount for the current billing period.
  final double currentBill;

  /// Total savings achieved compared to traditional agencies.
  final double savings;

  /// Invoices awaiting client approval.
  final List<BillingInvoice> pendingInvoices;

  /// History of paid invoices.
  final List<BillingInvoice> invoiceHistory;

  /// Breakdown of spending by category.
  final List<SpendingBreakdownItem> spendingBreakdown;

  /// Selected period for the breakdown.
  final BillingPeriod period;

  /// Error message if loading failed.
  final String? errorMessage;

  /// Creates a copy of this state with updated fields.
  BillingState copyWith({
    BillingStatus? status,
    double? currentBill,
    double? savings,
    List<BillingInvoice>? pendingInvoices,
    List<BillingInvoice>? invoiceHistory,
    List<SpendingBreakdownItem>? spendingBreakdown,
    BillingPeriod? period,
    String? errorMessage,
  }) {
    return BillingState(
      status: status ?? this.status,
      currentBill: currentBill ?? this.currentBill,
      savings: savings ?? this.savings,
      pendingInvoices: pendingInvoices ?? this.pendingInvoices,
      invoiceHistory: invoiceHistory ?? this.invoiceHistory,
      spendingBreakdown: spendingBreakdown ?? this.spendingBreakdown,
      period: period ?? this.period,
      errorMessage: errorMessage ?? this.errorMessage,
    );
  }

  @override
  List<Object?> get props => <Object?>[
    status,
    currentBill,
    savings,
    pendingInvoices,
    invoiceHistory,
    spendingBreakdown,
    period,
    errorMessage,
  ];
}
