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/vendor/domain/entities/vendor.dart';
import 'package:superport_v2/features/masters/vendor/presentation/dialogs/vendor_detail_dialog.dart';

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

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

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

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

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

    unawaited(
      showVendorDetailDialog(
        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('showVendorDetailDialog 상세 모드는 요약 정보를 제공한다', (tester) async {
    await tester.binding.setSurfaceSize(const Size(1280, 800));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    final vendor = Vendor(
      id: 7,
      vendorCode: 'V-007',
      vendorName: '슈퍼벤더',
      isActive: true,
      isDeleted: false,
      note: '테스트 벤더',
      createdAt: DateTime(2024, 3, 1, 9, 30),
      updatedAt: DateTime(2024, 3, 5, 12, 0),
    );

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

    unawaited(
      showVendorDetailDialog(
        context: context,
        dateFormat: dateFormat,
        vendor: vendor,
        onCreate: noopCreate,
        onUpdate: noopUpdate,
        onDelete: (id) async {
          expect(id, equals(7));
          return true;
        },
        onRestore: noopRestore,
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('벤더 상세'), findsWidgets);
    expect(find.text('슈퍼벤더'), findsWidgets);
    expect(find.text('삭제 상태'), findsOneWidget);
    expect(find.text('삭제'), findsOneWidget);
  });

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

    final vendor = Vendor(
      id: 9,
      vendorCode: 'V-009',
      vendorName: '삭제 대상',
      isActive: true,
      isDeleted: false,
      createdAt: DateTime(2024, 4, 1, 10),
      updatedAt: DateTime(2024, 4, 10, 18),
    );

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

    unawaited(
      showVendorDetailDialog(
        context: context,
        dateFormat: dateFormat,
        vendor: vendor,
        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);
  });
}
