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';

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,
    bool isCompact = false,
    bool isStale = false,
  }) {
    return MaterialApp(
      theme: buildKcTheme(),
      home: Scaffold(
        body: SizedBox(
          height: isCompact ? 72 : 200,
          width: isCompact ? 600 : 400,
          child: ProductDraftCard(
            draft: draft ?? sampleDraft,
            isSelected: isSelected,
            onTap: onTap,
            isCompact: isCompact,
            isStale: isStale,
          ),
        ),
      ),
    );
  }

  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);
    });

    // ── Description snippet (standard mode) ────────────────────────────

    testWidgets('standard mode shows description snippet', (tester) async {
      await tester.pumpWidget(buildTestWidget());
      expect(find.text('A test product'), findsOneWidget);
    });

    // ── Stale indicator ─────────────────────────────────────────────────

    testWidgets('shows stale indicator when isStale is true', (tester) async {
      await tester.pumpWidget(buildTestWidget(isStale: true));
      expect(find.byIcon(Icons.schedule), findsOneWidget);
    });

    testWidgets('hides stale indicator when isStale is false', (tester) async {
      await tester.pumpWidget(buildTestWidget(isStale: false));
      expect(find.byIcon(Icons.schedule), findsNothing);
    });

    // ── Compact mode ─────────────────────────────────────────────────────

    testWidgets('compact mode displays product name', (tester) async {
      await tester.pumpWidget(buildTestWidget(isCompact: true));
      expect(find.text('Test Bowl Cozy'), findsOneWidget);
    });

    testWidgets('compact mode displays SKU without prefix', (tester) async {
      await tester.pumpWidget(buildTestWidget(isCompact: true));
      // In compact mode, the SKU is shown without the "SKU: " prefix.
      expect(find.text('BC-TST-001'), findsOneWidget);
    });

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

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

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

    testWidgets('compact mode shows stale indicator when isStale is true', (tester) async {
      await tester.pumpWidget(buildTestWidget(isCompact: true, isStale: true));
      expect(find.byIcon(Icons.schedule), findsOneWidget);
    });

    testWidgets('compact mode hides stale indicator when isStale is false', (tester) async {
      await tester.pumpWidget(buildTestWidget(isCompact: true, isStale: false));
      expect(find.byIcon(Icons.schedule), findsNothing);
    });
  });
}
