import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ot_movil/models/work_order.dart';
import 'package:ot_movil/screens/work_order_details_screen.dart';

void main() {
  testWidgets('solicita cliente y dirección antes de mostrar las tareas', (
    tester,
  ) async {
    await tester.pumpWidget(const MaterialApp(home: _DetailsTestHost()));
    await tester.tap(find.text('Abrir datos'));
    await tester.pumpAndSettle();

    await tester.tap(find.byKey(const Key('continue-button')));
    await tester.pump();

    expect(find.text('Ingresa el nombre del cliente'), findsOneWidget);
    expect(find.text('Ingresa el teléfono del cliente'), findsOneWidget);
    expect(find.text('Ingresa la dirección del trabajo'), findsOneWidget);

    await tester.enterText(
      find.byKey(const Key('client-field')),
      'Comercial San Martín',
    );
    await tester.enterText(
      find.byKey(const Key('phone-field')),
      '+56 9 8765 4321',
    );
    await tester.enterText(
      find.byKey(const Key('address-field')),
      'Av. Central 450, Santiago',
    );
    await tester.tap(find.byKey(const Key('continue-button')));
    await tester.pumpAndSettle();

    expect(find.text('Comercial San Martín'), findsOneWidget);
    expect(find.text('+56 9 8765 4321'), findsOneWidget);
    expect(find.text('Av. Central 450, Santiago'), findsOneWidget);
  });
}

class _DetailsTestHost extends StatefulWidget {
  const _DetailsTestHost();

  @override
  State<_DetailsTestHost> createState() => _DetailsTestHostState();
}

class _DetailsTestHostState extends State<_DetailsTestHost> {
  WorkOrderDetails? _details;

  Future<void> _openDetails() async {
    final details = await Navigator.of(context).push<WorkOrderDetails>(
      MaterialPageRoute(builder: (_) => const WorkOrderDetailsScreen()),
    );
    if (details != null) setState(() => _details = details);
  }

  @override
  Widget build(BuildContext context) {
    final details = _details;
    if (details != null) {
      return Scaffold(
        body: Column(
          children: [
            Text(details.client),
            Text(details.phone),
            Text(details.address),
          ],
        ),
      );
    }
    return Scaffold(
      body: Center(
        child: FilledButton(
          onPressed: _openDetails,
          child: const Text('Abrir datos'),
        ),
      ),
    );
  }
}
