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

import 'package:superport_v2/features/masters/product/presentation/widgets/vendor_autocomplete_field.dart';
import 'package:superport_v2/features/masters/vendor/domain/entities/vendor.dart';

void main() {
  group('VendorAutocompleteField', () {
    testWidgets('선택한 항목이 입력창에 반영된다', (tester) async {
      final vendors = [
        Vendor(
          id: 1,
          vendorCode: 'V001',
          vendorName: '테스트 제조사',
          isActive: true,
          isDeleted: false,
        ),
      ];
      int? selectedId;

      await tester.pumpWidget(
        ShadApp(
          home: Scaffold(
            body: Center(
              child: VendorAutocompleteField(
                initialOptions: vendors,
                onSelected: (id) => selectedId = id,
              ),
            ),
          ),
        ),
      );

      // 포커스 후 옵션 선택
      await tester.tap(find.byType(ShadInput));
      await tester.pumpAndSettle();

      // RawAutocomplete 옵션은 Overlay에 그려진다.
      await tester.tap(find.text('테스트 제조사 (V001)'));
      await tester.pumpAndSettle();

      expect(selectedId, equals(1));

      final editableText = tester.widget<EditableText>(
        find.byType(EditableText),
      );
      expect(editableText.controller.text, '테스트 제조사 (V001)');
    });
  });
}
