import 'package:flutter/material.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/utils/currency_format.dart';
import 'package:niogu_app/core/utils/time_zone.dart';
import 'package:niogu_app/features/online_store/domain/entities/online_store.dart';
import 'package:niogu_app/features/online_store/presentation/widgets/product_thumbnail.dart';
import 'package:sizer/sizer.dart';

class OrderCard extends StatelessWidget {
  final OnlineOrder order;
  final VoidCallback onTap;
  const OrderCard({super.key, required this.order, required this.onTap});

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;
    return GestureDetector(
      onTap: onTap,
      child: Container(
        clipBehavior: Clip.antiAlias,
        padding: EdgeInsets.all(4.w),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(bottom: BorderSide(color: Colors.grey.shade100)),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.02),
              blurRadius: 5,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ProductThumbnail(imagePath: order.displayImage),
            SizedBox(width: 4.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        order.orderNumber,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1.25).sp
                              : (AppFontSize.small - 1.25).sp,
                        ),
                      ),
                      Text(
                        "${DateFormat('HH:mm').format(order.orderDate)} ${TimeZone.getCurrentTimeZone()}",
                        style: TextStyle(
                          color: Colors.grey.shade700,
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1.25).sp
                              : (AppFontSize.small - 1.25).sp,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 1.h),
                  _buildOrderInfoRow(
                    isTablet,
                    Icons.shopping_bag_outlined,
                    "${order.itemCount} Produk",
                  ),
                  SizedBox(height: 0.75.h),
                  _buildOrderInfoRow(
                    isTablet,
                    Icons.payments_outlined,
                    CurrencyFormat.formatToIdr(order.orderTotal, 0),
                    isPrice: true,
                  ),
                ],
              ),
            ),
            SizedBox(width: 2.w),
            Icon(Icons.chevron_right, size: 4.5.w, color: Colors.grey.shade400),
          ],
        ),
      ),
    );
  }

  Widget _buildOrderInfoRow(
    bool isTablet,
    IconData icon,
    String label, {
    bool isPrice = false,
  }) {
    return Row(
      children: [
        Icon(
          icon,
          size: 5.w,
          color: isPrice ? AppColor.primaryColor : Colors.grey,
        ),
        SizedBox(width: 2.w),
        Text(
          label,
          style: TextStyle(
            fontSize: isTablet
                ? (AppFontSize.medium - 1.25).sp
                : (AppFontSize.small - 1.25).sp,
            color: isPrice ? AppColor.primaryColor : Colors.grey[700],
            fontWeight: isPrice ? FontWeight.bold : FontWeight.normal,
          ),
        ),
      ],
    );
  }
}
