import 'dart:async';

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

import 'package:superport_v2/features/masters/group/domain/entities/group.dart';
import 'package:superport_v2/features/masters/group/presentation/dialogs/group_detail_dialog.dart';

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

void main() {
  final dateFormat = intl.DateFormat('yyyy-MM-dd HH:mm');

  Future<Group?> noopCreate(GroupInput _) async => null;
  Future<Group?> noopUpdate(int _, GroupInput __) async => null;
  Future<bool> noopDelete(int _) async => false;
  Future<Group?> noopRestore(int _) async => null;

  testWidgets('showGroupDetailDialog 등록 모드 폼을 노출한다', (tester) async {
    await tester.binding.setSurfaceSize(const Size(960, 720));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    await tester.pumpWidget(buildTestApp(const SizedBox.shrink()));
    final context = tester.element(find.byType(SizedBox));

    unawaited(
      showGroupDetailDialog(
        context: context,
        dateFormat: dateFormat,
        onCreate: noopCreate,
        onUpdate: noopUpdate,
        onDelete: noopDelete,
        onRestore: noopRestore,
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('그룹 등록'), findsOneWidget);
    expect(find.text('그룹명'), findsOneWidget);
    expect(find.text('설명'), findsOneWidget);
    expect(find.widgetWithText(ShadButton, '등록'), findsWidgets);
  });

  testWidgets('showGroupDetailDialog 상세 모드는 요약과 메타데이터를 제공한다', (tester) async {
    await tester.binding.setSurfaceSize(const Size(1280, 800));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    final group = Group(
      id: 12,
      groupName: '운영팀',
      description: '운영 담당',
      isDefault: false,
      isActive: true,
      isDeleted: false,
      note: '연중무휴',
      createdAt: DateTime(2024, 2, 1, 9),
      updatedAt: DateTime(2024, 3, 1, 10),
    );

    await tester.pumpWidget(buildTestApp(const SizedBox.shrink()));
    final context = tester.element(find.byType(SizedBox));

    unawaited(
      showGroupDetailDialog(
        context: context,
        dateFormat: dateFormat,
        group: group,
        onCreate: noopCreate,
        onUpdate: noopUpdate,
        onDelete: noopDelete,
        onRestore: noopRestore,
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('그룹 상세'), findsWidgets);
    expect(find.text('운영팀'), findsWidgets);
    expect(find.text('운영 담당'), findsWidgets);
    expect(find.text('기본 여부'), findsWidgets);
    expect(find.text('사용 여부'), findsWidgets);
    expect(find.text('삭제'), findsOneWidget);
  });

  testWidgets('삭제 섹션에서 경고와 버튼 상태를 노출한다', (tester) async {
    await tester.binding.setSurfaceSize(const Size(1280, 800));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    final group = Group(
      id: 20,
      groupName: '삭제 대상',
      isDefault: false,
      isActive: true,
      isDeleted: false,
      createdAt: DateTime(2024, 1, 10, 8),
      updatedAt: DateTime(2024, 1, 12, 9),
    );

    await tester.pumpWidget(buildTestApp(const SizedBox.shrink()));
    final context = tester.element(find.byType(SizedBox));

    unawaited(
      showGroupDetailDialog(
        context: context,
        dateFormat: dateFormat,
        group: group,
        onCreate: noopCreate,
        onUpdate: noopUpdate,
        onDelete: (_) async => true,
        onRestore: noopRestore,
      ),
    );

    await tester.pumpAndSettle();

    final deleteTab = find.text('삭제').first;
    await tester.tap(deleteTab, warnIfMissed: false);
    await tester.pumpAndSettle();

    expect(find.textContaining('삭제하면'), findsOneWidget);

    final dialog = find.byType(Dialog);
    final deleteButton = find
        .descendant(
          of: dialog,
          matching: find.widgetWithText(ShadButton, '삭제', skipOffstage: false),
        )
        .first;
    final buttonWidget = tester.widget<ShadButton>(deleteButton);
    expect(buttonWidget.onPressed, isNotNull);
  });
}
