import 'dart:async';

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

import 'package:superport_v2/widgets/components/superport_detail_dialog.dart';

import '../helpers/test_app.dart';

void main() {
  testWidgets('SuperportDetailDialog은 요약과 메타데이터를 렌더링한다', (tester) async {
    final widget = SuperportDetailDialog(
      summary: const Text('벤더 요약'),
      summaryBadges: const [ShadBadge(child: Text('활성'))],
      metadata: [
        SuperportDetailMetadata.text(label: 'ID', value: '#42'),
        SuperportDetailMetadata.text(label: '상태', value: '사용 중'),
      ],
      sections: [
        SuperportDetailDialogSection(
          id: 'overview',
          label: '개요',
          builder: (_) => const Text('개요 섹션'),
        ),
      ],
    );

    await tester.pumpWidget(buildTestApp(widget));
    await tester.pumpAndSettle();

    expect(find.text('벤더 요약'), findsOneWidget);
    expect(find.text('활성'), findsOneWidget);
    expect(find.text('ID'), findsOneWidget);
    expect(find.text('#42'), findsOneWidget);
    expect(find.text('상태'), findsOneWidget);
    expect(find.text('사용 중'), findsOneWidget);
    expect(find.text('개요 섹션'), findsOneWidget);
    expect(find.byType(ShadTabs), findsNothing);
  });

  testWidgets('SuperportDetailDialog은 여러 섹션 탭을 전환한다', (tester) async {
    final widget = SuperportDetailDialog(
      sections: [
        SuperportDetailDialogSection(
          id: 'overview',
          label: '개요',
          builder: (_) => const Text('개요 내용'),
        ),
        SuperportDetailDialogSection(
          id: 'history',
          label: '이력',
          builder: (_) => const Text('이력 내용'),
        ),
      ],
    );

    await tester.pumpWidget(buildTestApp(widget));
    await tester.pumpAndSettle();

    final tabsFinder = find.byWidgetPredicate((widget) => widget is ShadTabs);
    expect(tabsFinder, findsOneWidget);
    expect(find.text('개요 내용'), findsOneWidget);
    expect(find.text('이력 내용'), findsNothing);

    await tester.tap(find.text('이력'));
    await tester.pumpAndSettle();

    expect(find.text('개요 내용'), findsNothing);
    expect(find.text('이력 내용'), findsOneWidget);
  });

  testWidgets('showSuperportDetailDialog는 기본 닫기 버튼을 렌더링하지 않는다', (tester) async {
    await tester.pumpWidget(buildTestApp(const SizedBox.shrink()));
    await tester.pumpAndSettle();

    final context = tester.element(find.byType(SizedBox));

    unawaited(
      showSuperportDetailDialog<void>(
        context: context,
        title: '테스트 다이얼로그',
        sections: [
          SuperportDetailDialogSection(
            id: 'overview',
            label: '개요',
            builder: (_) => const Text('본문'),
          ),
        ],
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('테스트 다이얼로그'), findsOneWidget);
    expect(find.text('본문'), findsOneWidget);
    expect(find.text('닫기'), findsNothing);
  });
}
