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

import 'package:superport_v2/core/config/environment.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/outbound/presentation/pages/outbound_page.dart';

import '../../helpers/inventory_test_stubs.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  setUpAll(() async {
    await Environment.initialize();
  });

  setUp(() {
    registerInventoryTestStubs(
      const InventoryTestStubConfig(registerProductRepository: true),
    );
  });

  tearDown(() async {
    await GetIt.I.reset();
  });

  testWidgets('출고 등록 모달은 번호 입력 없이 자동 생성 안내를 제공한다', (tester) async {
    final view = tester.view;
    view.physicalSize = const Size(1280, 900);
    view.devicePixelRatio = 1.0;
    addTearDown(() {
      view.resetPhysicalSize();
      view.resetDevicePixelRatio();
    });

    await tester.pumpWidget(
      MaterialApp(
        home: ScaffoldMessenger(
          child: PermissionScope(
            manager: PermissionManager(),
            child: ShadTheme(
              data: SuperportShadTheme.light(),
              child: Scaffold(
                body: OutboundPage(routeUri: Uri.parse('/inventory/outbound')),
              ),
            ),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    await tester.tap(find.widgetWithText(ShadButton, '출고 등록'));
    await tester.pumpAndSettle();

    expect(find.text('저장 시 자동 생성'), findsAtLeastNWidgets(2));

    await tester.tap(find.widgetWithText(ShadButton, '저장'));
    await tester.pump();

    expect(find.text('거래번호를 입력하세요.'), findsNothing);
    expect(find.text('결재번호를 입력하세요.'), findsNothing);
    expect(find.textContaining('자동완성에서 선택'), findsOneWidget);
    expect(find.text('창고를 선택하세요.'), findsOneWidget);
  });

  testWidgets('출고 상세의 수정 버튼은 수정 모달을 연다', (tester) async {
    final view = tester.view;
    view.physicalSize = const Size(1280, 900);
    view.devicePixelRatio = 1.0;
    addTearDown(() {
      view.resetPhysicalSize();
      view.resetDevicePixelRatio();
    });

    await tester.pumpWidget(
      MaterialApp(
        home: ScaffoldMessenger(
          child: PermissionScope(
            manager: PermissionManager(),
            child: ShadTheme(
              data: SuperportShadTheme.light(),
              child: Scaffold(
                body: OutboundPage(routeUri: Uri.parse('/inventory/outbound')),
              ),
            ),
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    await tester.tap(find.text('TX-20240302-010').first);
    await tester.pumpAndSettle();

    expect(find.text('출고 상세'), findsOneWidget);

    final editButton = find.widgetWithText(ShadButton, '수정').last;
    await tester.ensureVisible(editButton);
    await tester.tap(editButton);
    await tester.pump();
    await tester.pumpAndSettle(const Duration(milliseconds: 500));

    expect(find.text('출고 수정'), findsOneWidget);
    expect(find.widgetWithText(ShadButton, '저장'), findsWidgets);
  });
}
