import 'package:core/core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:kell_mobile/app.dart';
import 'package:kell_mobile/composition/mobile_app_services.dart';

Widget _buildTestApp() {
  const config = KcAppConfig(
    environment: KcAppEnvironment.fake,
    wcSiteUrl: '',
    wcConsumerKey: '',
    wcConsumerSecret: '',
  );
  return KcAppScope<MobileAppServices>(
    services: MobileAppServices.fake(),
    config: config,
    child: const KellMobileApp(),
  );
}

void main() {
  testWidgets('mobile shell loads with dashboard tab', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    expect(find.text('Dashboard'), findsWidgets);
  });

  testWidgets('dashboard shows summary cards after loading', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    expect(find.text('Overview'), findsOneWidget);
    expect(find.text('Total Products'), findsOneWidget);
    expect(find.text('In Stock'), findsOneWidget);
  });

  testWidgets('environment badge shows FAKE in fake mode', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    expect(find.text('FAKE'), findsOneWidget);
  });

  testWidgets('bottom navigation bar has 5 destinations', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    expect(find.byType(NavigationBar), findsOneWidget);
    expect(find.byType(NavigationDestination), findsNWidgets(5));
  });

  testWidgets('tapping Inventory tab switches content', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Tap the Inventory destination
    await tester.tap(find.text('Inventory').last);
    await tester.pumpAndSettle();

    // The app bar title should change
    expect(find.text('Inventory'), findsWidgets);
  });

  testWidgets('tapping More tab shows additional sections', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Tap the More destination
    await tester.tap(find.text('More').last);
    await tester.pumpAndSettle();

    expect(find.text('Finance'), findsOneWidget);
    expect(find.text('Policy'), findsOneWidget);
    expect(find.text('Integrations'), findsOneWidget);
  });

  // ── Mobile Publishing Surface tests ──────────────────────────────

  testWidgets('Products tab shows mobile publishing page with search bar', (
    WidgetTester tester,
  ) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Tap the Products destination
    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Should show the search bar
    expect(find.byType(TextField), findsOneWidget);
    expect(find.text('Search products…'), findsOneWidget);
  });

  testWidgets('Products tab shows filter chips', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Should show filter chips for status categories
    expect(find.widgetWithText(FilterChip, 'All'), findsOneWidget);
    expect(find.widgetWithText(FilterChip, 'Draft'), findsOneWidget);
    expect(find.widgetWithText(FilterChip, 'Published'), findsOneWidget);
  });

  testWidgets('Products tab shows product count', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Fake data should produce a product count label
    expect(find.textContaining('products'), findsWidgets);
  });

  testWidgets('Products tab shows sort button', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Should have a sort icon/button
    expect(find.byIcon(Icons.sort), findsOneWidget);
  });

  // ── Stage 6A: Android feedback and action polish tests ────────────

  testWidgets('tapping product card navigates to detail page', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Navigate to Products tab
    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Tap the first product card in the list
    final cards = find.byType(GestureDetector);
    expect(cards, findsWidgets);

    // Tap on the first product in the list view
    final firstProduct = find.byWidgetPredicate(
      (widget) => widget is SizedBox && widget.height == 72 && widget.child != null,
    );
    if (firstProduct.evaluate().isNotEmpty) {
      await tester.tap(firstProduct.first);
      await tester.pumpAndSettle();

      // Should now be on the detail page — has an AppBar with a back button
      expect(find.byType(AppBar), findsOneWidget);
    }
  });

  testWidgets('detail page shows confirmation dialog for publish action', (
    WidgetTester tester,
  ) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Navigate to Products tab
    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Tap the first 72-height card to navigate to detail
    final firstProduct = find.byWidgetPredicate(
      (widget) => widget is SizedBox && widget.height == 72 && widget.child != null,
    );
    if (firstProduct.evaluate().isNotEmpty) {
      await tester.tap(firstProduct.first);
      await tester.pumpAndSettle();

      // Scroll down to reveal the status action button
      await tester.drag(find.byType(SingleChildScrollView), const Offset(0, -300));
      await tester.pumpAndSettle();

      // Look for Publish or Move to Draft button
      final publishButton = find.text('Publish to Store');
      final moveToDraftButton = find.text('Move to Draft');

      if (publishButton.evaluate().isNotEmpty) {
        await tester.tap(publishButton);
        await tester.pumpAndSettle();

        // Confirmation dialog should appear
        expect(find.text('Publish?'), findsOneWidget);
        expect(find.text('Cancel'), findsOneWidget);
        expect(find.text('Publish'), findsOneWidget);

        // Cancel should dismiss the dialog
        await tester.tap(find.text('Cancel'));
        await tester.pumpAndSettle();

        // Dialog should be gone
        expect(find.text('Publish?'), findsNothing);
      } else if (moveToDraftButton.evaluate().isNotEmpty) {
        await tester.tap(moveToDraftButton);
        await tester.pumpAndSettle();

        // Confirmation dialog should appear
        expect(find.text('Move to Draft?'), findsOneWidget);
        expect(find.text('Cancel'), findsOneWidget);

        // Cancel should dismiss the dialog
        await tester.tap(find.text('Cancel'));
        await tester.pumpAndSettle();

        // Dialog should be gone
        expect(find.text('Move to Draft?'), findsNothing);
      }
    }
  });

  testWidgets('detail page shows product name in app bar', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Navigate to Products tab
    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Tap the first product card
    final firstProduct = find.byWidgetPredicate(
      (widget) => widget is SizedBox && widget.height == 72 && widget.child != null,
    );
    if (firstProduct.evaluate().isNotEmpty) {
      await tester.tap(firstProduct.first);
      await tester.pumpAndSettle();

      // Detail page AppBar should show the product name
      // The AppBar should exist and have a title
      expect(find.byType(AppBar), findsOneWidget);
    }
  });

  testWidgets('detail page back navigation returns to product list', (WidgetTester tester) async {
    await tester.pumpWidget(_buildTestApp());
    await tester.pumpAndSettle();

    // Navigate to Products tab
    await tester.tap(find.text('Products').last);
    await tester.pumpAndSettle();

    // Tap the first product card
    final firstProduct = find.byWidgetPredicate(
      (widget) => widget is SizedBox && widget.height == 72 && widget.child != null,
    );
    if (firstProduct.evaluate().isNotEmpty) {
      await tester.tap(firstProduct.first);
      await tester.pumpAndSettle();

      // Press back (the leading back button in AppBar)
      final backButton = find.byType(BackButton);
      if (backButton.evaluate().isNotEmpty) {
        await tester.tap(backButton);
        await tester.pumpAndSettle();

        // Should be back on the product list with search bar
        expect(find.text('Search products…'), findsOneWidget);
      }
    }
  });
}
