import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/router/app_route.dart';
import 'package:niogu_app/core/enums/order_source.dart';
import 'package:niogu_app/core/enums/order_status.dart';
import 'package:niogu_app/core/utils/time_zone.dart';
import 'package:niogu_app/features/customer/domain/entities/customer.dart';
import 'package:niogu_app/features/online_store/presentation/providers/online_store_provider.dart';
import 'package:niogu_app/features/report/transaction/presentation/providers/transaction_report_provider.dart';
import 'package:sizer/sizer.dart';

class CustomerActivity extends ConsumerWidget {
  final bool isOnlineCustomer;
  final CustomerActivities activity;
  const CustomerActivity({
    super.key,
    required this.isOnlineCustomer,
    required this.activity,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final bool isTablet = 100.w >= 600;

    final status = activity.orderStatus;

    final Color colorBadge = switch (status) {
      OrderStatus.pending_payment => Colors.red.shade50,
      OrderStatus.pending_confirmation => Colors.grey.shade50,
      OrderStatus.confirmed => Colors.orange.shade50,
      OrderStatus.in_progress => Colors.blue.shade50,
      OrderStatus.completed => Colors.green.shade50,
      OrderStatus.canceled => Colors.red.shade50,
    };

    final String orderStatus = switch (status) {
      OrderStatus.pending_payment => "Verifikasi Pembayaran",
      OrderStatus.pending_confirmation => "Tertunda",
      OrderStatus.confirmed => "Dikonfirmasi",
      OrderStatus.in_progress => "Pengiriman",
      OrderStatus.completed => "Selesai",
      OrderStatus.canceled => "Dibatalkan",
    };

    final Color colorStatus = switch (status) {
      OrderStatus.pending_payment => Colors.red.shade700,
      OrderStatus.pending_confirmation => Colors.grey.shade700,
      OrderStatus.confirmed => Colors.orange.shade700,
      OrderStatus.in_progress => Colors.blue.shade700,
      OrderStatus.completed => Colors.green.shade700,
      OrderStatus.canceled => Colors.red.shade700,
    };

    return GestureDetector(
      onTap: () async {
        if (activity.orderSource == OrderSource.pos) {
          final orderDetail = await ref
              .read(transactionReportRepositoryProvider)
              .getOrderDetail(activity.id);

          FocusScope.of(context).unfocus();

          await Future.delayed(const Duration(milliseconds: 800));

          context.pushNamed(
            AppRoute.transactionReportOrderDetailScreen,
            extra: orderDetail,
          );
        } else {
          final onlineOrderDetail = await ref
              .read(onlineStoreRepositoryProvider)
              .fetchOnlineOrderDetailById(activity.id);

          FocusScope.of(context).unfocus();

          await Future.delayed(const Duration(milliseconds: 800), () {
            context.pushNamed(
              AppRoute.onlineStoreOrderDetilScreen,
              extra: onlineOrderDetail,
            );
          });
        }
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 1.5.h),
        padding: EdgeInsets.all(4.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(2.5.w),
          border: Border.all(color: Colors.grey[200]!),
        ),
        child: Row(
          children: [
            Container(
              padding: EdgeInsets.all(2.w),
              decoration: BoxDecoration(
                color: AppColor.primaryColor.withOpacity(0.1),
                shape: BoxShape.circle,
              ),
              child: Icon(
                Icons.shopping_bag_outlined,
                color: AppColor.primaryColor,
                size: 5.w,
              ),
            ),
            SizedBox(width: 4.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    activity.orderNumber,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1.25).sp
                          : (AppFontSize.small - 1.25).sp,
                    ),
                  ),
                  SizedBox(height: 0.75.h),
                  Text(
                    "Via ${isOnlineCustomer ? 'Toko Online' : 'Kasir Offline'}",
                    style: TextStyle(
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1.25).sp
                          : (AppFontSize.small - 1.25).sp,
                      color: Colors.grey,
                    ),
                  ),
                  SizedBox(height: 0.75.h),
                  Container(
                    margin: EdgeInsets.only(top: 0.5.h),
                    padding: EdgeInsets.symmetric(
                      horizontal: 2.w,
                      vertical: 0.5.w,
                    ),
                    decoration: BoxDecoration(
                      color: colorBadge,
                      borderRadius: BorderRadius.circular(1.w),
                    ),
                    child: Text(
                      orderStatus,
                      style: TextStyle(
                        fontSize: isTablet
                            ? (AppFontSize.medium - 1.25).sp
                            : (AppFontSize.small - 1.25).sp,
                        fontWeight: FontWeight.bold,
                        color: colorStatus,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Text(
              "${DateFormat("dd MMMM yyyy HH:mm").format(activity.orderDate)} ${TimeZone.getCurrentTimeZone()}",
              style: TextStyle(
                fontSize: isTablet
                    ? (AppFontSize.medium - 1.25).sp
                    : (AppFontSize.small - 1.25).sp,
                color: Colors.grey,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
