import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart';
import '../blocs/billing_bloc.dart';
import '../blocs/billing_event.dart';
import '../blocs/billing_state.dart';
import '../widgets/billing_header.dart';
import '../widgets/pending_invoices_section.dart';
import '../widgets/payment_method_card.dart';
import '../widgets/spending_breakdown_card.dart';
import '../widgets/savings_card.dart';
import '../widgets/invoice_history_section.dart';
import '../widgets/export_invoices_button.dart';

/// The entry point page for the client billing feature.
///
/// This page initializes the [BillingBloc] and provides it to the [BillingView].
class BillingPage extends StatelessWidget {
  /// Creates a [BillingPage].
  const BillingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider<BillingBloc>(
      create: (BuildContext context) =>
          Modular.get<BillingBloc>()..add(const BillingLoadStarted()),
      child: const BillingView(),
    );
  }
}

/// The main view for the client billing feature.
///
/// This widget displays the billing dashboard content based on the current
/// state of the [BillingBloc].
class BillingView extends StatelessWidget {
  /// Creates a [BillingView].
  const BillingView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<BillingBloc, BillingState>(
      builder: (BuildContext context, BillingState state) {
        return Scaffold(
          backgroundColor: UiColors.bgPrimary,
          body: Column(
            children: <Widget>[
              BillingHeader(
                currentBill: state.currentBill,
                savings: state.savings,
                onBack: () => Modular.to.pop(),
              ),
              Expanded(child: _buildContent(context, state)),
            ],
          ),
        );
      },
    );
  }

  Widget _buildContent(BuildContext context, BillingState state) {
    if (state.status == BillingStatus.loading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (state.status == BillingStatus.failure) {
      return Center(
        child: Text(
          state.errorMessage ?? 'An error occurred',
          style: UiTypography.body1r.textError,
        ),
      );
    }

    return SingleChildScrollView(
      padding: const EdgeInsets.all(UiConstants.space5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          if (state.pendingInvoices.isNotEmpty) ...<Widget>[
            PendingInvoicesSection(invoices: state.pendingInvoices),
            const SizedBox(height: UiConstants.space4),
          ],
          const PaymentMethodCard(),
          const SizedBox(height: UiConstants.space4),
          const SpendingBreakdownCard(),
          const SizedBox(height: UiConstants.space4),
          SavingsCard(savings: state.savings),
          const SizedBox(height: UiConstants.space6),
          InvoiceHistorySection(invoices: state.invoiceHistory),
          const SizedBox(height: UiConstants.space6),
          const ExportInvoicesButton(),
          const SizedBox(height: UiConstants.space6),
        ],
      ),
    );
  }
}
