import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:krow_data_connect/krow_data_connect.dart';

import 'data/repositories/view_orders_repository_impl.dart';
import 'domain/repositories/i_view_orders_repository.dart';
import 'domain/usecases/get_accepted_applications_for_day_use_case.dart';
import 'domain/usecases/get_orders_use_case.dart';
import 'presentation/blocs/view_orders_cubit.dart';
import 'presentation/pages/view_orders_page.dart';

/// Module for the View Orders feature.
///
/// This module sets up Dependency Injection for repositories, use cases,
/// and BLoCs, and defines the feature's navigation routes.
class ViewOrdersModule extends Module {
  @override
  List<Module> get imports => <Module>[DataConnectModule()];

  @override
  void binds(Injector i) {
    // Repositories
    i.add<IViewOrdersRepository>(ViewOrdersRepositoryImpl.new);

    // UseCases
    i.add(GetOrdersUseCase.new);
    i.add(GetAcceptedApplicationsForDayUseCase.new);

    // BLoCs
    i.add(ViewOrdersCubit.new);
  }

  @override
  void routes(RouteManager r) {
    r.child(
      '/',
      child: (BuildContext context) {
        final Object? args = Modular.args.data;
        DateTime? initialDate;
        if (args is DateTime) {
          initialDate = args;
        } else if (args is Map<String, dynamic>) {
          final Object? rawDate = args['initialDate'];
          if (rawDate is DateTime) {
            initialDate = rawDate;
          } else if (rawDate is String) {
            initialDate = DateTime.tryParse(rawDate);
          }
        }
        return ViewOrdersPage(initialDate: initialDate);
      },
    );
  }
}
