import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart' as intl;
import 'package:superport_v2/features/masters/product/domain/entities/product.dart';
import 'package:superport_v2/features/masters/product/presentation/dialogs/product_detail_dialog.dart';
import 'package:superport_v2/features/masters/vendor/domain/entities/vendor.dart';
import 'package:superport_v2/features/masters/uom/domain/entities/uom.dart';

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

void main() {
  final dateFormat = intl.DateFormat('yyyy-MM-dd HH:mm');
  final vendorOptions = [
    Vendor(id: 1, vendorCode: 'V-001', vendorName: '슈퍼벤더'),
  ];
  final uomOptions = [Uom(id: 1, uomName: 'EA')];

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

  testWidgets('showProductDetailDialog 등록 모드는 입력 폼을 표시한다', (tester) async {
    await tester.pumpWidget(buildTestApp(const SizedBox.shrink()));
    final context = tester.element(find.byType(SizedBox));

    unawaited(
      showProductDetailDialog(
        context: context,
        dateFormat: dateFormat,
        vendorOptions: vendorOptions,
        uomOptions: uomOptions,
        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.text('단위'), findsOneWidget);
    expect(find.text('등록'), findsWidgets);
  });

  testWidgets('showProductDetailDialog 상세 모드는 기본/연결/히스토리 정보를 제공한다', (
    tester,
  ) async {
    await tester.binding.setSurfaceSize(const Size(1280, 800));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    final product = Product(
      id: 42,
      productCode: 'P-100',
      productName: '테스트 제품',
      vendor: ProductVendor(id: 1, vendorCode: 'V-001', vendorName: '슈퍼벤더'),
      uom: ProductUom(id: 1, uomName: 'EA'),
      isActive: true,
      isDeleted: false,
      note: '비고 메모',
      createdAt: DateTime(2024, 1, 1, 9),
      updatedAt: DateTime(2024, 2, 2, 10),
    );

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

    unawaited(
      showProductDetailDialog(
        context: context,
        dateFormat: dateFormat,
        product: product,
        vendorOptions: vendorOptions,
        uomOptions: uomOptions,
        onCreate: noopCreate,
        onUpdate: noopUpdate,
        onDelete: noopDelete,
        onRestore: noopRestore,
      ),
    );

    await tester.pumpAndSettle();

    expect(find.text('제품 상세'), findsOneWidget);
    expect(find.text('테스트 제품'), findsWidgets);
    expect(find.text('제품코드'), findsOneWidget);
    expect(find.text('연결 관계'), findsOneWidget);

    await tester.tap(find.text('연결 관계'));
    await tester.pumpAndSettle();
    expect(find.textContaining('슈퍼벤더'), findsWidgets);

    await tester.tap(find.text('히스토리'));
    await tester.pumpAndSettle();
    expect(find.textContaining('변경 이력 데이터는 준비 중입니다.'), findsOneWidget);
  });
}
