import 'package:drift/native.dart' show NativeDatabase;
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:minstrel/cache/audio_cache_manager.dart' show appDbProvider;
import 'package:minstrel/cache/db.dart';
import 'package:minstrel/library/widgets/album_card.dart';
import 'package:minstrel/library/widgets/track_row.dart';
import 'package:minstrel/models/album.dart';
import 'package:minstrel/models/track.dart';
import 'package:minstrel/theme/theme_data.dart';

void main() {
  testWidgets('AlbumCard renders title and artist', (tester) async {
    await tester.pumpWidget(MaterialApp(
      theme: buildThemeData(),
      home: Scaffold(
        body: AlbumCard(
          album: const AlbumRef(
            id: 'a',
            title: 'Geogaddi',
            artistId: 'x',
            artistName: 'Boards of Canada',
          ),
          onTap: () {},
        ),
      ),
    ));
    expect(find.text('Geogaddi'), findsOneWidget);
    expect(find.text('Boards of Canada'), findsOneWidget);
  });

  testWidgets('TrackRow shows mm:ss duration', (tester) async {
    // TrackRow contains CachedIndicator (ConsumerWidget) which reaches
    // audioCacheManagerProvider → appDbProvider. Override appDbProvider
    // with an explicit NativeDatabase.memory() executor — drift_flutter's
    // default `driftDatabase()` schedules a deferred-init Timer that
    // outlives the test widget tree and trips
    // _verifyInvariants("A Timer is still pending after dispose").
    await tester.pumpWidget(ProviderScope(
      overrides: [
        appDbProvider.overrideWith((ref) {
          final db = AppDb(NativeDatabase.memory());
          ref.onDispose(db.close);
          return db;
        }),
      ],
      child: MaterialApp(
        theme: buildThemeData(),
        home: Scaffold(
          body: TrackRow(
            track: const TrackRef(
              id: 't',
              title: 'Roygbiv',
              albumId: 'a',
              artistId: 'x',
              durationSec: 137,
              trackNumber: 4,
            ),
            onTap: () {},
          ),
        ),
      ),
    ));
    expect(find.text('02:17'), findsOneWidget);
  });
}
