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

import 'package:superport_v2/core/network/api_error.dart';
import 'package:superport_v2/core/network/failure.dart';
import 'package:superport_v2/features/inventory/shared/widgets/warehouse_select_field.dart';
import 'package:superport_v2/features/masters/warehouse/domain/repositories/warehouse_repository.dart';

class _MockWarehouseRepository extends Mock implements WarehouseRepository {}

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  final getIt = GetIt.instance;

  setUp(() async {
    await getIt.reset();
  });

  tearDown(() async {
    await getIt.reset();
  });

  testWidgets('창고 로드 실패 시 Failure 메시지를 표시한다', (tester) async {
    final repository = _MockWarehouseRepository();
    getIt.registerSingleton<WarehouseRepository>(repository);

    final exception = ApiException(
      code: ApiErrorCode.unknown,
      message: '창고 목록을 불러오지 못했습니다.',
      details: {
        'errors': {
          'warehouse': ['창고 데이터를 조회할 수 없습니다.'],
        },
      },
    );

    when(
      () => repository.list(
        page: 1,
        pageSize: 100,
        isActive: true,
        includeZipcode: false,
      ),
    ).thenThrow(exception);

    await tester.pumpWidget(
      MaterialApp(
        home: ShadTheme(
          data: ShadThemeData(
            colorScheme: const ShadSlateColorScheme.light(),
            brightness: Brightness.light,
          ),
          child: Scaffold(
            body: Center(
              child: InventoryWarehouseSelectField(onChanged: (_) {}),
            ),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    final failure = Failure.from(exception);
    expect(find.text(failure.describe()), findsOneWidget);
  });
}
