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

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

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

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

  testWidgets('showCustomerDetailDialog 등록 모드 폼을 렌더링한다', (tester) async {
    await tester.pumpWidget(buildTestApp(const SizedBox.shrink()));
    final context = tester.element(find.byType(SizedBox));

    unawaited(
      showCustomerDetailDialog(
        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.text('등록'), findsWidgets);
  });

  testWidgets('showCustomerDetailDialog 상세 모드는 고객 요약을 표시한다', (tester) async {
    await tester.binding.setSurfaceSize(const Size(1280, 800));
    addTearDown(() => tester.binding.setSurfaceSize(null));

    final customer = Customer(
      id: 10,
      customerCode: 'C-001',
      customerName: '슈퍼고객',
      contactName: '홍길동',
      isPartner: true,
      isGeneral: false,
      email: 'customer@example.com',
      mobileNo: '010-1111-2222',
      zipcode: CustomerZipcode(
        zipcode: '06000',
        sido: '서울',
        sigungu: '강남구',
        roadName: '테헤란로',
      ),
      addressDetail: '101호',
      isActive: true,
      isDeleted: false,
      note: 'VIP',
      createdAt: DateTime(2024, 3, 1, 9),
      updatedAt: DateTime(2024, 3, 2, 10),
    );

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

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

    await tester.pumpAndSettle();

    expect(find.text('고객사 상세'), findsOneWidget);
    expect(find.textContaining('슈퍼고객').first, findsOneWidget);
    expect(find.text('고객사 코드'), findsWidgets);
    expect(find.text('유형'), findsWidgets);
  });
}
