import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../core/utils/currency_utils.dart';
import '../cart/cart_state.dart';

final trackProvider = FutureProvider.autoDispose.family<Map<String, dynamic>?, String>((ref, orderId) {
  return ref.watch(publicApiProvider).trackOrder(orderId);
});

class TrackScreen extends ConsumerWidget {
  const TrackScreen({super.key, required this.orderId});

  final String orderId;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final trackAsync = ref.watch(trackProvider(orderId));

    return Directionality(
      textDirection: TextDirection.rtl,
      child: Scaffold(
        appBar: AppBar(title: const Text('پیگیری سفارش')),
        body: trackAsync.when(
          data: (order) {
            if (order == null) return const Center(child: Text('سفارش یافت نشد'));
            final status = order['status'] as String? ?? '';
            final items = order['items'] as List<dynamic>? ?? [];
            return Padding(
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Text('وضعیت: $status', style: Theme.of(context).textTheme.titleLarge),
                  Text('مبلغ: ${formatToman((order['total'] as num).toInt())}'),
                  const SizedBox(height: 16),
                  const Text('اقلام:'),
                  ...items.map((i) => Text('• ${i['menuItemName']} × ${i['quantity']}')),
                ],
              ),
            );
          },
          loading: () => const Center(child: CircularProgressIndicator()),
          error: (e, _) => Center(child: Text('خطا: $e')),
        ),
      ),
    );
  }
}
