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

import 'package:doormile/main.dart';
import 'package:doormile/shared/state/app_state.dart';
import 'package:doormile/core/theme/app_colors.dart';
import 'package:doormile/core/theme/app_theme.dart';
import 'package:doormile/core/widgets/common.dart';

class BookingSuccessScreen extends StatefulWidget {
  const BookingSuccessScreen({super.key});

  @override
  State<BookingSuccessScreen> createState() => _BookingSuccessScreenState();
}

class _BookingSuccessScreenState extends State<BookingSuccessScreen>
    with SingleTickerProviderStateMixin {
  late final AnimationController _c =
      AnimationController(vsync: this, duration: const Duration(milliseconds: 600))..forward();

  @override
  void dispose() {
    _c.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final order = context.read<AppState>().orders.first;
    return Scaffold(
      backgroundColor: AppColors.surface,
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            children: [
              const Spacer(),
              ScaleTransition(
                scale: CurvedAnimation(parent: _c, curve: Curves.elasticOut),
                child: Container(
                  width: 110,
                  height: 110,
                  decoration: BoxDecoration(
                    color: AppColors.success.withValues(alpha: 0.12),
                    shape: BoxShape.circle,
                  ),
                  child: const Icon(Icons.check_circle,
                      color: AppColors.success, size: 72),
                ),
              ),
              const SizedBox(height: 28),
              Text('Booking Confirmed!', style: AppTheme.headlineLg),
              const SizedBox(height: 8),
              Text(
                'Your EV pickup is scheduled. We’ll notify you when the rider is on the way.',
                textAlign: TextAlign.center,
                style: AppTheme.bodyMd.copyWith(color: AppColors.onSurfaceVariant),
              ),
              const SizedBox(height: 24),
              AppCard(
                child: Column(
                  children: [
                    _row('Order ID', order.id),
                    const Divider(height: 22, color: AppColors.surfaceContainer),
                    _row('Estimated Fare', '₹${order.amount.toStringAsFixed(0)}'),
                    const Divider(height: 22, color: AppColors.surfaceContainer),
                    _row('Pickup Status', 'Waiting for Miler assignment'),
                  ],
                ),
              ),
              const SizedBox(height: 16),
              const EcoBanner(
                  text: 'This delivery is 100% electric — offsetting ~1.2kg of CO₂.'),
              const Spacer(),
              PrimaryButton(
                label: 'Track Live',
                trailingIcon: Icons.navigation,
                onPressed: () => Navigator.pushNamedAndRemoveUntil(
                  context,
                  Routes.liveTracking,
                  (r) => r.settings.name == Routes.home,
                  arguments: order,
                ),
              ),
              const SizedBox(height: 12),
              TextButton(
                onPressed: () => Navigator.popUntil(
                    context, (r) => r.settings.name == Routes.home),
                child: Text('Back to Home',
                    style: AppTheme.labelMd.copyWith(color: AppColors.primary)),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _row(String k, String v) => Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(k, style: AppTheme.bodyMd.copyWith(color: AppColors.onSurfaceVariant)),
          const SizedBox(width: 12),
          Flexible(
            child: Text(v,
                textAlign: TextAlign.right,
                style: AppTheme.labelMd.copyWith(fontWeight: FontWeight.w700)),
          ),
        ],
      );
}
