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

import 'package:superport_v2/features/approvals/request/presentation/controllers/approval_request_controller.dart';
import 'package:superport_v2/features/approvals/request/presentation/widgets/approval_step_configurator.dart';

Widget _buildTestApp(Widget child) {
  return MaterialApp(
    home: ShadTheme(
      data: ShadThemeData(
        colorScheme: const ShadSlateColorScheme.light(),
        brightness: Brightness.light,
      ),
      child: Scaffold(body: child),
    ),
  );
}

ApprovalRequestParticipant _participant(int id, String name) {
  return ApprovalRequestParticipant(id: id, name: name, employeeNo: 'EMP$id');
}

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('요약 섹션에 상신자와 단계 정보가 표시된다', (tester) async {
    final controller = ApprovalRequestController();
    controller.setRequester(_participant(1, '상신자'));
    controller.addStep(approver: _participant(2, '1차 승인자'));
    controller.addStep(approver: _participant(3, '최종 승인자'));

    await tester.pumpWidget(
      _buildTestApp(ApprovalStepConfigurator(controller: controller)),
    );
    await tester.pumpAndSettle();

    expect(find.textContaining('상신자: 상신자'), findsOneWidget);
    expect(find.textContaining('최종 승인자: 최종 승인자'), findsOneWidget);
    expect(find.textContaining('총 단계: 2개'), findsOneWidget);
  });

  testWidgets('편집 버튼을 누르면 구성 모달이 열린다', (tester) async {
    final controller = ApprovalRequestController();
    controller.setRequester(_participant(1, '사용자A'));

    await tester.pumpWidget(
      _buildTestApp(ApprovalStepConfigurator(controller: controller)),
    );
    await tester.pump();

    await tester.tap(find.text('단계 구성 편집'));
    await tester.pumpAndSettle();

    expect(find.text('결재 단계 구성'), findsWidgets);
    expect(find.text('결재 단계 목록'), findsOneWidget);
  });
}
