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

import 'package:doormile/state/app_state.dart';
import 'package:doormile/theme/app_theme.dart';
import 'package:doormile/screens/onboarding_screen.dart';
import 'package:doormile/screens/login_screen.dart';
import 'package:doormile/screens/otp_screen.dart';
import 'package:doormile/screens/main_shell.dart';
import 'package:doormile/screens/home_screen.dart';
import 'package:doormile/screens/wallet_screen.dart';
import 'package:doormile/screens/profile_screen.dart';
import 'package:doormile/screens/help_support_screen.dart';
import 'package:doormile/screens/booking/booking_pickup_screen.dart';
import 'package:doormile/screens/booking/parcel_details_screen.dart';
import 'package:doormile/screens/booking/service_price_screen.dart';
import 'package:doormile/screens/booking/booking_preview_screen.dart';
import 'package:doormile/screens/booking/booking_success_screen.dart';
import 'package:doormile/screens/tracking/my_orders_screen.dart';
import 'package:doormile/screens/tracking/live_tracking_screen.dart';
import 'package:doormile/screens/tracking/shipment_journey_screen.dart';

/// Renders each screen in isolation at a phone viewport and settles it, so the
/// test framework flags any overflow in the *settled* layout (no transition
/// transients). A pumped order is placed first so success/tracking have data.
Future<void> _renderScreen(WidgetTester tester, Widget child) async {
  final state = AppState()
    ..login()
    ..setPhone('98765 43210');
  state.placeOrder();
  await tester.pumpWidget(
    ChangeNotifierProvider<AppState>.value(
      value: state,
      child: MaterialApp(theme: AppTheme.light, home: child),
    ),
  );
  await tester.pump(const Duration(milliseconds: 400));
  await tester.pump(const Duration(milliseconds: 400));
}

void main() {
  setUp(() {});

  final screens = <String, Widget Function()>{
    'Onboarding': () => const OnboardingScreen(),
    'Login': () => const LoginScreen(),
    'OTP': () => const OtpScreen(),
    'MainShell/Home': () => const MainShell(),
    'Home': () => const Scaffold(body: HomeScreen()),
    'Wallet': () => const Scaffold(body: WalletScreen()),
    'Profile': () => const Scaffold(body: ProfileScreen()),
    'Help': () => const HelpSupportScreen(),
    'BookingPickup': () => const BookingPickupScreen(),
    'ParcelDetails': () => const ParcelDetailsScreen(),
    'ServicePrice': () => const ServicePriceScreen(),
    'BookingPreview': () => const BookingPreviewScreen(),
    'BookingSuccess': () => const BookingSuccessScreen(),
    'MyOrders': () => const MyOrdersScreen(),
    'LiveTracking': () => const LiveTrackingScreen(),
    'ShipmentJourney': () => const ShipmentJourneyScreen(),
  };

  for (final entry in screens.entries) {
    testWidgets('${entry.key} renders cleanly at phone size', (tester) async {
      tester.view.physicalSize = const Size(1170, 2532);
      tester.view.devicePixelRatio = 3.0;
      addTearDown(tester.view.reset);
      await _renderScreen(tester, entry.value());
      expect(tester.takeException(), isNull);
    });
  }
}
