import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xp_dashboard/src/widgets/progress_card.dart';
import 'package:xp_models/xp_models.dart';

void main() {
  group('ProgressCard Widget Tests', () {
    testWidgets('displays progress correctly', (WidgetTester tester) async {
      // Create test data
      final stats = DashboardStats(
        today: TodayStats(
          level: 5,
          xp: 1250,
          focusTime: 14400, // 4 hours
          meetingTime: 7200, // 2 hours
          focusSessions: 4,
        ),
        streaks: StreakStats(
          currentStreak: 7,
          longestStreak: 15,
        ),
        recentActivity: [],
        timestamp: DateTime.now(),
      );

      // Build the widget
      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: ProgressCard(stats: stats),
          ),
        ),
      );

      // Verify the progress items are displayed
      expect(find.text('Today\'s Progress'), findsOneWidget);
      expect(find.text('Focus Time'), findsOneWidget);
      expect(find.text('Meeting Time'), findsOneWidget);
      expect(find.text('Focus Sessions'), findsOneWidget);
      
      // Check formatted durations
      expect(find.text('4h 0m'), findsOneWidget); // Focus time
      expect(find.text('2h 0m'), findsOneWidget); // Meeting time
      expect(find.text('4'), findsOneWidget); // Focus sessions
      
      // Check progress percentages
      expect(find.text('50% of target'), findsOneWidget); // Focus time (4/8 hours)
      expect(find.text('50% of target'), findsAtLeastNWidgets(2)); // Meeting time (2/4 hours) and focus sessions (4/8)
    });

    testWidgets('handles null stats gracefully', (WidgetTester tester) async {
      await tester.pumpWidget(
        const MaterialApp(
          home: Scaffold(
            body: ProgressCard(stats: null),
          ),
        ),
      );

      // Should show loading indicator
      expect(find.byType(CircularProgressIndicator), findsOneWidget);
      expect(find.text('Today\'s Progress'), findsOneWidget);
    });

    testWidgets('formats duration correctly', (WidgetTester tester) async {
      final stats = DashboardStats(
        today: TodayStats(
          level: 1,
          xp: 100,
          focusTime: 3665, // 1 hour, 1 minute, 5 seconds
          meetingTime: 90, // 1 minute, 30 seconds
          focusSessions: 1,
        ),
        streaks: StreakStats(
          currentStreak: 1,
          longestStreak: 1,
        ),
        recentActivity: [],
        timestamp: DateTime.now(),
      );

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: ProgressCard(stats: stats),
          ),
        ),
      );

      // Check formatted durations (should truncate seconds)
      expect(find.text('1h 1m'), findsOneWidget); // Focus time
      expect(find.text('0h 1m'), findsOneWidget); // Meeting time
    });

    testWidgets('calculates progress percentages correctly', (WidgetTester tester) async {
      final stats = DashboardStats(
        today: TodayStats(
          level: 1,
          xp: 100,
          focusTime: 28800, // 8 hours (100% of target)
          meetingTime: 14400, // 4 hours (100% of target)
          focusSessions: 8, // 8 sessions (100% of target)
        ),
        streaks: StreakStats(
          currentStreak: 1,
          longestStreak: 1,
        ),
        recentActivity: [],
        timestamp: DateTime.now(),
      );

      await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: ProgressCard(stats: stats),
          ),
        ),
      );

      // All should show 100% of target
      expect(find.text('100% of target'), findsNWidgets(3));
    });
  });
}
