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

import 'package:doormile/main.dart';

/// Drives the app through its main flows at a phone-sized viewport to catch
/// layout overflows and runtime exceptions on each screen.
void main() {
  testWidgets('full user journey renders without errors', (WidgetTester tester) async {
    tester.view.physicalSize = const Size(1170, 2532); // iPhone-ish @3x
    tester.view.devicePixelRatio = 3.0;
    addTearDown(tester.view.reset);

    await tester.pumpWidget(const DoormileApp());

    // Splash -> Onboarding
    await tester.pump(const Duration(seconds: 3));
    await tester.pumpAndSettle();
    expect(find.text('Send a parcel in one touch'), findsOneWidget);

    // Skip onboarding -> Login
    await tester.tap(find.text('Skip'));
    await tester.pumpAndSettle();
    expect(find.text('Welcome back'), findsOneWidget);

    // Enter phone and request OTP
    await tester.enterText(find.byType(TextField).first, '9876543210');
    await tester.pump();
    await tester.tap(find.text('Send OTP'));
    await tester.pump(const Duration(milliseconds: 1400));
    await tester.pumpAndSettle();
    expect(find.text('Verify your number'), findsOneWidget);

    // Fill the 6 OTP boxes
    final otpBoxes = find.byType(TextField);
    for (int i = 0; i < 6; i++) {
      await tester.enterText(otpBoxes.at(i), '1');
      await tester.pump();
    }
    await tester.tap(find.text('Verify & Continue'));
    await tester.pump(const Duration(milliseconds: 1300));
    await tester.pumpAndSettle();

    // Home
    expect(find.text('Send a parcel'), findsOneWidget);

    // Start a booking
    await tester.tap(find.text('Book Now'));
    await tester.pumpAndSettle();
    expect(find.text('Step 1: Confirm Pickup'), findsOneWidget);

    await tester.tap(find.text('Continue'));
    await tester.pumpAndSettle();
    expect(find.text('What are you sending?'), findsOneWidget);

    await tester.tap(find.text('Continue'));
    await tester.pumpAndSettle();
    expect(find.text('CHOOSE SERVICE'), findsOneWidget);

    // Confirm & Continue -> review
    await tester.tap(find.textContaining('Confirm & Continue'));
    await tester.pumpAndSettle();
    expect(find.text('Review Booking'), findsWidgets);

    // Pop all the way back to the home shell.
    for (int i = 0; i < 4; i++) {
      if (find.text('Send a parcel').evaluate().isNotEmpty) break;
      await tester.pageBack();
      await tester.pumpAndSettle();
    }
    expect(find.text('Send a parcel'), findsOneWidget);

    // Wallet tab
    await tester.tap(find.text('Wallet').first);
    await tester.pumpAndSettle();
    expect(find.text('Doormile Wallet'), findsOneWidget);

    // Profile tab
    await tester.tap(find.text('Profile').first);
    await tester.pumpAndSettle();
    expect(find.text('Riya Sharma'), findsOneWidget);

    // Track tab (orders list)
    await tester.tap(find.text('Track').first);
    await tester.pumpAndSettle();
    expect(find.text('My Parcels'), findsOneWidget);
  });
}
