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

import 'package:feature_wordpress/feature_wordpress.dart';
import 'package:feature_wordpress/src/presentation/widgets/product_draft_card.dart';

void main() {
  final sampleDraft = ProductDraft(
    id: '1',
    name: 'Test Bowl Cozy',
    description: 'A test product',
    price: 12.99,
    sku: 'BC-TST-001',
    category: 'Bowl Cozies',
    imageUrl: '',
    status: PublishStatus.draft,
    lastModified: DateTime(2026, 4, 1),
  );

  Widget buildTestWidget({ProductDraft? draft, bool isSelected = false, VoidCallback? onTap}) {
    return MaterialApp(
      theme: buildKcTheme(),
      home: Scaffold(
        body: SizedBox(
          height: 200,
          width: 400,
          child: ProductDraftCard(
            draft: draft ?? sampleDraft,
            isSelected: isSelected,
            onTap: onTap,
          ),
        ),
      ),
    );
  }

  group('ProductDraftCard', () {
    testWidgets('displays product name', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('Test Bowl Cozy'), findsOneWidget);
    });

    testWidgets('displays SKU', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('SKU: BC-TST-001'), findsOneWidget);
    });

    testWidgets('displays price', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('\$12.99'), findsOneWidget);
    });

    testWidgets('displays category', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('Bowl Cozies'), findsOneWidget);
    });

    testWidgets('displays status chip', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('Draft'), findsOneWidget);
    });

    testWidgets('displays last modified date', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('2026-04-01'), findsOneWidget);
    });

    testWidgets('formats single-digit month and day with leading zeros', (tester) async {
      final janDraft = sampleDraft.copyWith(lastModified: DateTime(2026, 1, 5));
      await tester.pumpWidget(buildTestWidget(draft: janDraft));
      expect(find.text('2026-01-05'), findsOneWidget);
    });

    testWidgets('calls onTap when tapped', (tester) async {
      var tapped = false;
      await tester.pumpWidget(buildTestWidget(onTap: () => tapped = true));
      await tester.tap(find.text('Test Bowl Cozy'));
      expect(tapped, true);
    });
  });
}
