import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/components/top_back_bar_app.dart';
import 'package:niogu_app/core/router/app_route.dart';
import 'package:niogu_app/features/online_store/presentation/providers/online_store_provider.dart';
import 'package:niogu_app/features/online_store/presentation/widgets/domain_card.dart';
import 'package:niogu_app/features/online_store/presentation/widgets/sub_menu_card.dart';
import 'package:sizer/sizer.dart';

class OnlineStoreScreen extends ConsumerWidget {
  const OnlineStoreScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return LayoutBuilder(
      builder: (context, constraints) {
        int badgeCount = 0;

        final badgeCountState = ref.watch(
          numberOfNeedToBeProcessedStreamProvider,
        );

        badgeCountState.whenData((count) => badgeCount = count);

        return SafeArea(
          top: false,
          bottom: true,
          right: false,
          left: false,
          child: Scaffold(
            backgroundColor: const Color(0xFFF9FAFB),
            appBar: TopBackBarApp(
              title: "Toko Online",
              onTap: () => Navigator.pop(context),
            ),
            body: SingleChildScrollView(
              padding: EdgeInsets.all(5.w),
              child: Column(
                children: [
                  DomainCard(domainName: "queenfruits.niogu.com"),

                  SizedBox(height: 3.h),

                  SubMenuCard(
                    title: "Atur Toko Online",
                    subtitle: "Kelola profil, deskripsi, dan tampilan toko.",
                    icon: Icons.settings_outlined,
                    onTap: () async {
                      final configure = await ref
                          .read(onlineStoreRepositoryProvider)
                          .getConfigureOnlineStore();

                      await Future.delayed(
                        const Duration(milliseconds: 800),
                        () {
                          context.pushNamed(
                            AppRoute.configureStoreOnlineScreen,
                            extra: configure,
                          );
                        },
                      );
                    },
                  ),
                  SubMenuCard(
                    title: "Pesanan Dari Toko Online",
                    subtitle: "Lihat dan proses pesanan yang masuk.",
                    icon: Icons.shopping_bag_outlined,
                    onTap: () {
                      context.pushNamed(AppRoute.onlineStoreOrderScreen);
                    },
                    badgeCount: badgeCount > 0 ? badgeCount : null,
                  ),
                  SubMenuCard(
                    title: "Atur Biaya Pengiriman",
                    subtitle: "Konfigurasi tarif pengiriman.",
                    icon: Icons.local_shipping_outlined,
                    onTap: () async {
                      final configuration = await ref
                          .read(onlineStoreRepositoryProvider)
                          .fetchShippingConfiguration();

                      await Future.delayed(
                        const Duration(milliseconds: 800),
                        () {
                          context.pushNamed(
                            AppRoute.shippingConfigurationScreen,
                            extra: configuration,
                          );
                        },
                      );
                    },
                  ),
                  SubMenuCard(
                    title: "Atur Rekening/E-Wallet",
                    subtitle: "Kelola metode pembayaran pelanggan.",
                    icon: Icons.account_balance_wallet_outlined,
                    onTap: () async {
                      final configures = await ref
                          .read(onlineStoreRepositoryProvider)
                          .fetchConfigurePaymentMethod();

                      await Future.delayed(
                        const Duration(milliseconds: 800),
                        () {
                          context.pushNamed(
                            AppRoute.paymentMethodScreen,
                            extra: configures,
                          );
                        },
                      );
                    },
                  ),
                  SubMenuCard(
                    title: "Review Produk",
                    subtitle: "Pantau ulasan dan feedback pelanggan.",
                    icon: Icons.reviews_outlined,
                    onTap: () {
                      context.pushNamed(AppRoute.productReviewScreen);
                    },
                  ),
                  SubMenuCard(
                    title: "Atur Rating Produk",
                    subtitle: "Kelola tampilan ulasan dan feedback pelanggan",
                    icon: Icons.star_outline,
                    onTap: () {
                      context.pushNamed(AppRoute.ratingConfigurationScreen);
                    },
                  ),
                  SubMenuCard(
                    title: "Buat Campaign",
                    subtitle: "Tingkatkan penjualan dengan promo menarik.",
                    icon: Icons.campaign_outlined,
                    onTap: () {
                      context.pushNamed(AppRoute.campaignScreen);
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
