import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hyper_local/l10n/app_localizations.dart';
import 'package:hyper_local/screens/my_orders/model/order_detail_model.dart';
import 'package:hyper_local/screens/my_orders/model/order_status.dart';
import 'package:intl/intl.dart';

class OrderStatusHeroCard extends StatelessWidget {
  final OrderDetailData order;
  final OrderStatus status;

  const OrderStatusHeroCard({
    super.key,
    required this.order,
    required this.status,
  });

  @override
  Widget build(BuildContext context) {
    final palette = status.palette(context);
    final dark = Theme.of(context).brightness == Brightness.dark;
    final l10n = AppLocalizations.of(context);

    final headline = _buildHeadline(context, l10n);
    final secondary = _buildSecondary(context, l10n);
    final pill = _statusLabel(l10n);

    return Container(
      width: double.infinity,
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            palette.bg,
            Color.lerp(palette.bg, palette.accent, dark ? 0.18 : 0.12)!,
          ],
        ),
        borderRadius: BorderRadius.circular(16.r),
        border: Border.all(
          color: palette.accent.withValues(alpha: dark ? 0.25 : 0.18),
          width: 1,
        ),
        boxShadow: [
          BoxShadow(
            color: palette.accent.withValues(alpha: dark ? 0.12 : 0.08),
            blurRadius: 16,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Stack(
        children: [
          // Decorative background icon
          Positioned(
            right: -8.w,
            bottom: -10.h,
            child: Icon(
              status.icon,
              size: 72.sp,
              color: palette.accent.withValues(alpha: dark ? 0.10 : 0.08),
            ),
          ),
          // Content
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 14.h),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    // Frosted-glass status pill
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 4.h),
                      decoration: BoxDecoration(
                        color: dark
                            ? palette.fg.withValues(alpha: 0.15)
                            : Colors.white.withValues(alpha: 0.75),
                        borderRadius: BorderRadius.circular(999.r),
                        border: Border.all(
                          color: palette.fg.withValues(alpha: dark ? 0.20 : 0.12),
                          width: 0.5,
                        ),
                      ),
                      child: Text(
                        pill,
                        style: TextStyle(
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w700,
                          color: palette.fg,
                          letterSpacing: 0.5,
                        ),
                      ),
                    ),
                    if (order.id != null)
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 3.h),
                        decoration: BoxDecoration(
                          color: palette.accent.withValues(alpha: dark ? 0.15 : 0.10),
                          borderRadius: BorderRadius.circular(6.r),
                        ),
                        child: Text(
                          '#${order.id}',
                          style: TextStyle(
                            fontSize: 10.sp,
                            fontWeight: FontWeight.w600,
                            color: palette.accent,
                          ),
                        ),
                      ),
                  ],
                ),
                SizedBox(height: 10.h),
                Text(
                  headline,
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.bold,
                    color: palette.fg,
                    height: 1.25,
                  ),
                ),
                SizedBox(height: 4.h),
                Text(
                  secondary,
                  style: TextStyle(
                    fontSize: 11.5.sp,
                    fontWeight: FontWeight.w400,
                    color: palette.accent,
                    height: 1.3,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  String _statusLabel(AppLocalizations? l10n) {
    if (l10n == null) return status.name.toUpperCase();
    switch (status) {
      case OrderStatus.placed:
        return l10n.statusPlaced;
      case OrderStatus.partiallyAccepted:
        return l10n.statusPartiallyAccepted;
      case OrderStatus.partiallyRejected:
        return l10n.statusPartiallyRejected;
      case OrderStatus.confirmed:
        return l10n.statusConfirmed;
      case OrderStatus.preparing:
        return l10n.statusPreparing;
      case OrderStatus.readyForPickup:
        return l10n.statusReadyForPickup;
      case OrderStatus.assigned:
        return l10n.statusAssigned;
      case OrderStatus.shipped:
        return l10n.statusShipped;
      case OrderStatus.outForDelivery:
        return l10n.statusOutForDelivery;
      case OrderStatus.delivered:
        return l10n.statusDelivered;
      case OrderStatus.cancelled:
        return l10n.statusCancelled;
      case OrderStatus.returned:
        return l10n.statusReturned;
      case OrderStatus.failed:
        return l10n.statusFailed;
      case OrderStatus.unknown:
        return status.name.toUpperCase();
    }
  }

  String _buildHeadline(BuildContext context, AppLocalizations? l10n) {
    final eta = _formattedEta();
    final isLate = _isRunningLate();
    final isPickup = order.deliveryType?.toLowerCase() == 'pickup' ||
        order.fulfillmentType?.toLowerCase() == 'pickup';

    if (isLate && status.isInTransit && !isPickup) {
      return l10n?.heroRunningLate ?? 'Running a bit late';
    }

    switch (status) {
      case OrderStatus.placed:
        return l10n?.heroAwaitingConfirmation ?? 'Awaiting confirmation';
      case OrderStatus.partiallyAccepted:
        return l10n?.heroPartiallyAccepted ?? 'Some items unavailable';
      case OrderStatus.partiallyRejected:
        return l10n?.heroPartiallyRejected ?? 'Order partially rejected';
      case OrderStatus.confirmed:
      case OrderStatus.preparing:
      case OrderStatus.shipped:
      case OrderStatus.outForDelivery:
        if (isPickup) {
          return l10n?.heroStorePreparing ?? 'Store is preparing your order';
        }
        if (eta != null) {
          return l10n?.heroArrivingBy(eta) ?? 'Arriving by $eta';
        }
        return l10n?.heroOnTheWay ?? 'On the way';
      case OrderStatus.readyForPickup:
        return l10n?.heroReadyForPickup ?? 'Your order is ready for pickup';
      case OrderStatus.assigned:
        return l10n?.heroAssigned ?? 'Delivery partner assigned';
      case OrderStatus.delivered:
        if (isPickup) {
          return l10n?.heroDelivered ?? 'Delivered';
        }
        return _wasDeliveredOnTime()
            ? (l10n?.heroDeliveredOnTime ?? 'Delivered on time')
            : (l10n?.heroDelivered ?? 'Delivered');
      case OrderStatus.cancelled:
        return l10n?.heroOrderCancelled ?? 'Order cancelled';
      case OrderStatus.returned:
        return l10n?.heroReturnedAndRefunded ?? 'Returned & refunded';
      case OrderStatus.failed:
        return l10n?.heroPaymentFailed ?? 'Payment failed';
      case OrderStatus.unknown:
        final rawStatus = order.status ?? '';
        if (rawStatus.isEmpty) return '';
        return rawStatus.split('_').map((word) {
          if (word.isEmpty) return '';
          return word[0].toUpperCase() + word.substring(1).toLowerCase();
        }).join(' ');
    }
  }

  String _buildSecondary(BuildContext context, AppLocalizations? l10n) {
    final placedTime = _formattedTime(order.createdAt);
    final eta = _formattedEta();
    final isPickup = order.deliveryType?.toLowerCase() == 'pickup' ||
        order.fulfillmentType?.toLowerCase() == 'pickup';

    switch (status) {
      case OrderStatus.placed:
        if (placedTime != null && eta != null && !isPickup) {
          return l10n?.secondaryPlacedAndEta(placedTime, eta) ??
              'Placed $placedTime · ETA $eta';
        }
        return l10n?.secondaryUsuallyAccepted ?? 'Usually accepted in 2 min';
      case OrderStatus.partiallyAccepted:
      case OrderStatus.partiallyRejected:
        final rejectedCount = order.items
            .where((i) => i.status == 'rejected' || i.status == 'cancelled')
            .length;
        final acceptedCount = order.items
            .where((i) => i.status == 'accepted')
            .length;
        final preparingCount = order.items
            .where((i) => i.status == 'preparing')
            .length;
        final pendingCount = order.items
            .where((i) =>
                i.status == 'pending' ||
                i.status == 'awaiting_store_response')
            .length;
        final parts = <String>[
          if (acceptedCount > 0)
            l10n?.itemCountAccepted(acceptedCount) ?? '$acceptedCount accepted',
          if (preparingCount > 0)
            l10n?.itemCountPreparing(preparingCount) ?? '$preparingCount preparing',
          if (rejectedCount > 0)
            l10n?.itemCountRejected(rejectedCount) ?? '$rejectedCount rejected',
          if (pendingCount > 0)
            l10n?.itemCountPending(pendingCount) ?? '$pendingCount pending',
        ];
        if (parts.isNotEmpty) return parts.join(' · ');
        return l10n?.secondaryPartialItems(order.items.length) ??
            '${order.items.length} items';
      case OrderStatus.confirmed:
      case OrderStatus.preparing:
        if (isPickup) {
          return 'We will notify you when it\'s ready for pickup';
        }
        return l10n?.heroStorePreparing ?? 'Store is preparing your order';
      case OrderStatus.readyForPickup:
        return l10n?.secondaryReadyForPickup ?? 'Head to the store to collect your order';
      case OrderStatus.assigned:
        return l10n?.secondaryAssigned ?? 'A delivery partner is heading to pick up your order';
      case OrderStatus.shipped:
      case OrderStatus.outForDelivery:
        return l10n?.heroOnTheWay ?? 'On the way';
      case OrderStatus.delivered:
        final date = _formattedDate(order.updatedAt ?? order.createdAt);
        final time = _formattedTime(order.updatedAt ?? order.createdAt);
        if (date != null && time != null) {
          return l10n?.secondaryDeliveredOn(date, time) ?? '$date · $time';
        }
        return '';
      case OrderStatus.cancelled:
      case OrderStatus.failed:
        final date = _formattedDate(order.updatedAt ?? order.createdAt);
        if (date != null) {
          final isStoreCancel = order.status?.toLowerCase().contains('store') == true ||
                                order.status?.toLowerCase().contains('seller') == true ||
                                order.status?.toLowerCase().contains('reject') == true;
          if (isStoreCancel) {
            return l10n?.secondaryCancelledByStore(date) ?? 'Cancelled by store · $date';
          }
          return l10n?.secondaryCancelledByYou(date) ?? 'Cancelled · $date';
        }
        return '';
      case OrderStatus.returned:
        final date = _formattedDate(order.updatedAt ?? order.createdAt);
        final count = order.items.length;
        if (date != null) {
          return l10n?.secondaryItemsReturned(count, date) ??
              '$count items returned · $date';
        }
        return '';
      case OrderStatus.unknown:
        return '';
    }
  }

  bool _isRunningLate() {
    final eta = _etaDateTime();
    if (eta == null) return false;
    return DateTime.now().isAfter(eta);
  }

  bool _wasDeliveredOnTime() {
    final eta = _etaDateTime();
    final deliveredAt = order.updatedAt;
    if (eta == null || deliveredAt == null) return true;
    final delivered = DateTime.tryParse(deliveredAt);
    if (delivered == null) return true;
    return delivered
        .isBefore(eta.add(const Duration(minutes: 15)));
  }

  DateTime? _etaDateTime() {
    final created = order.createdAt;
    final minutes = order.estimatedDeliveryTime;
    if (created == null || minutes == null) return null;
    final base = DateTime.tryParse(created);
    if (base == null) return null;
    return base.add(Duration(minutes: minutes));
  }

  String? _formattedEta() {
    final eta = _etaDateTime();
    if (eta == null) return null;
    return DateFormat('h:mm a').format(eta.toLocal());
  }

  String? _formattedTime(String? raw) {
    if (raw == null) return null;
    final dt = DateTime.tryParse(raw);
    if (dt == null) return null;
    return DateFormat('h:mm a').format(dt.toLocal());
  }

  String? _formattedDate(String? raw) {
    if (raw == null) return null;
    final dt = DateTime.tryParse(raw);
    if (dt == null) return null;
    return DateFormat('d MMM').format(dt.toLocal());
  }
}
