import 'package:block_seasons/data/save_repository.dart';
import 'package:block_seasons/game/models/season.dart';
import 'package:block_seasons/game/models/stage.dart';
import 'package:block_seasons/l10n/gen/app_localizations.dart';
import 'package:block_seasons/state/providers.dart';
import 'package:block_seasons/ui/screens/home_screen.dart';
import 'package:block_seasons/ui/screens/season_title_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

SeasonPack _pack() => SeasonPack(
      schemaVersion: 1,
      seasonId: 'season_001',
      version: 1,
      title: const {'en': 'First Bloom', 'ko': '첫 개화'},
      theme: SeasonTheme.fallback,
      stages: [
        StageConfig(
          id: 's1',
          seed: 1,
          moveLimit: 10,
          preset: const [],
          objectives: const [],
          stars: const StarThresholds(twoMovesLeft: 2, threeMovesLeft: 4),
          generatorProfile: 'mid',
        ),
      ],
    );

Widget _app(SaveRepository repo) => ProviderScope(
      overrides: [
        saveRepositoryProvider.overrideWithValue(repo),
        seasonsProvider.overrideWith((ref) async => [_pack()]),
      ],
      child: const MaterialApp(
        localizationsDelegates: AppLocalizations.localizationsDelegates,
        supportedLocales: AppLocalizations.supportedLocales,
        home: SeasonTitleScreen(),
      ),
    );

void main() {
  testWidgets('shows season title then auto-advances to home',
      (tester) async {
    SharedPreferences.setMockInitialValues({});
    final repo = SaveRepository(await SharedPreferences.getInstance());
    await tester.pumpWidget(_app(repo));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));
    expect(find.text('First Bloom'), findsOneWidget);
    await tester.pump(const Duration(seconds: 2));
    await tester.pumpAndSettle();
    expect(find.byType(HomeScreen), findsOneWidget);
  });

  testWidgets('tap skips immediately', (tester) async {
    SharedPreferences.setMockInitialValues({});
    final repo = SaveRepository(await SharedPreferences.getInstance());
    await tester.pumpWidget(_app(repo));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 100));
    await tester.tap(find.text('First Bloom'));
    await tester.pumpAndSettle();
    expect(find.byType(HomeScreen), findsOneWidget);
  });
}
