import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:superport_v2/core/permissions/permission_manager.dart';
import 'package:superport_v2/core/theme/superport_shad_theme.dart';
import 'package:superport_v2/features/inventory/inbound/presentation/pages/inbound_page.dart';
import 'package:superport_v2/features/inventory/outbound/presentation/pages/outbound_page.dart';
import 'package:superport_v2/features/inventory/rental/presentation/pages/rental_page.dart';

Widget _wrapInventoryPage(Widget child) {
  return PermissionScope(
    manager: PermissionManager(),
    child: ShadApp(
      theme: SuperportShadTheme.light(),
      darkTheme: SuperportShadTheme.dark(),
      home: Scaffold(body: child),
    ),
  );
}

void main() {
  testWidgets(
    'Inbound page reflects include state from route and closes dialog with Esc',
    (tester) async {
      final view = tester.view;
      view.physicalSize = const Size(1280, 800);
      view.devicePixelRatio = 1.0;
      addTearDown(() {
        view.resetPhysicalSize();
        view.resetDevicePixelRatio();
      });

      await tester.pumpWidget(
        _wrapInventoryPage(
          InboundPage(routeUri: Uri.parse('/inventory/inbound?include=lines')),
        ),
      );
      await tester.pumpAndSettle();

      expect(find.text('상세정보 포함'), findsWidgets);
      expect(tester.takeException(), isNull);

      await tester.tap(find.widgetWithText(ShadButton, '입고 등록'));
      await tester.pumpAndSettle();
      expect(find.byType(Dialog), findsOneWidget);

      await tester.sendKeyEvent(LogicalKeyboardKey.escape);
      await tester.pumpAndSettle();
      expect(find.byType(Dialog), findsNothing);
    },
  );

  testWidgets('Outbound page include selection updates UI', (tester) async {
    final view = tester.view;
    view.physicalSize = const Size(1280, 800);
    view.devicePixelRatio = 1.0;
    addTearDown(() {
      view.resetPhysicalSize();
      view.resetDevicePixelRatio();
    });

    await tester.pumpWidget(
      _wrapInventoryPage(
        OutboundPage(
          routeUri: Uri.parse('/inventory/outbound?include=lines,customers'),
        ),
      ),
    );
    await tester.pumpAndSettle();

    expect(find.text('상세정보 포함'), findsWidgets);
    expect(find.text('고객 포함'), findsWidgets);
    expect(tester.takeException(), isNull);
  });

  testWidgets('Rental page reacts to include toggle', (tester) async {
    final view = tester.view;
    view.physicalSize = const Size(1280, 800);
    view.devicePixelRatio = 1.0;
    addTearDown(() {
      view.resetPhysicalSize();
      view.resetDevicePixelRatio();
    });

    await tester.pumpWidget(
      _wrapInventoryPage(
        RentalPage(routeUri: Uri.parse('/inventory/rental?include=lines')),
      ),
    );
    await tester.pumpAndSettle();

    expect(find.text('상세정보 포함'), findsWidgets);
    expect(tester.takeException(), isNull);
  });
}
