import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';

import 'package:smplayer/providers/appearance_provider.dart';
import 'package:smplayer/router/app_router.dart';
import 'package:smplayer/router/page_transitions.dart';

void main() {
  group('player route transition regression', () {
    late GoRouter router;

    setUp(() {
      final container = ProviderContainer(
        overrides: [initialLocationProvider.overrideWithValue('/')],
      );
      router = container.read(appRouterProvider);
    });

    test('player route uses platform-aware player transition', () {
      GoRoute? playerRoute;
      for (final route in router.configuration.routes) {
        if (route is GoRoute && route.path == '/player/:itemId') {
          playerRoute = route;
          break;
        }
        for (final sub in route.routes) {
          if (sub is GoRoute && sub.path == '/player/:itemId') {
            playerRoute = sub;
            break;
          }
        }
        if (playerRoute != null) break;
      }

      expect(
        playerRoute,
        isNotNull,
        reason: '/player/:itemId route must exist',
      );

      final page = playerRoute!.pageBuilder!(
        _FakeBuildContext(),
        GoRouterState(
          router.configuration,
          uri: Uri.parse('/player/test123'),
          matchedLocation: '/player/test123',
          pathParameters: const {'itemId': 'test123'},
          fullPath: '/player/:itemId',
          pageKey: const ValueKey('/player/test123'),
        ),
      );

      expect(
        page,
        isA<CustomTransitionPage>(),
        reason: 'player route must use a custom player transition',
      );
      final transitionPage = page as CustomTransitionPage;
      expect(
        transitionPage.transitionsBuilder,
        same(PageTransitions.player),
        reason: 'player route must avoid horizontal motion on Android',
      );
      expect(transitionPage.transitionDuration, PageTransitions.playerForward);
      expect(
        transitionPage.reverseTransitionDuration,
        PageTransitions.playerReverse,
      );
    });
  });
}

class _FakeBuildContext extends Fake implements BuildContext {}
