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

import 'package:minstrel/api/endpoints/playlists.dart';
import 'package:minstrel/models/playlist.dart';
import 'package:minstrel/playlists/playlists_provider.dart';
import 'package:minstrel/shared/widgets/track_actions/add_to_playlist_sheet.dart';
import 'package:minstrel/theme/theme_data.dart';

const _userPlaylist = Playlist(
  id: 'p1',
  userId: 'u1',
  name: 'Road trip',
  description: '',
  isPublic: false,
  systemVariant: null,
  trackCount: 12,
  coverUrl: '',
  ownerUsername: 'alice',
  createdAt: '2026-05-01T00:00:00Z',
  updatedAt: '2026-05-01T00:00:00Z',
);

const _systemPlaylist = Playlist(
  id: 'p2',
  userId: 'u1',
  name: 'For You',
  description: '',
  isPublic: false,
  systemVariant: 'for_you',
  trackCount: 75,
  coverUrl: '',
  ownerUsername: 'alice',
  createdAt: '2026-05-01T00:00:00Z',
  updatedAt: '2026-05-01T00:00:00Z',
);

Widget _harness(PlaylistsList lists) {
  return ProviderScope(
    overrides: [
      playlistsListProvider('user').overrideWith((ref) => Stream.value(lists)),
    ],
    child: MaterialApp(
      theme: buildThemeData(),
      home: Builder(builder: (ctx) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () => AddToPlaylistSheet.show(ctx),
              child: const Text('open'),
            ),
          ),
        );
      }),
    ),
  );
}

void main() {
  testWidgets('renders user playlists, hides system ones', (t) async {
    await t.pumpWidget(_harness(
      const PlaylistsList(owned: [_userPlaylist, _systemPlaylist], public: []),
    ));
    await t.tap(find.text('open'));
    await t.pumpAndSettle();
    expect(find.text('Road trip'), findsOneWidget);
    expect(find.text('For You'), findsNothing);
  });

  testWidgets('empty state when no user playlists', (t) async {
    await t.pumpWidget(_harness(
      const PlaylistsList(owned: [_systemPlaylist], public: []),
    ));
    await t.tap(find.text('open'));
    await t.pumpAndSettle();
    expect(
      find.text("You haven't created any playlists yet."),
      findsOneWidget,
    );
  });

  testWidgets('tapping a row pops with playlist id', (t) async {
    String? picked;
    await t.pumpWidget(ProviderScope(
      overrides: [
        playlistsListProvider('user').overrideWith(
          (ref) => Stream.value(
              const PlaylistsList(owned: [_userPlaylist], public: [])),
        ),
      ],
      child: MaterialApp(
        theme: buildThemeData(),
        home: Builder(builder: (ctx) {
          return Scaffold(
            body: Center(
              child: ElevatedButton(
                onPressed: () async {
                  picked = await AddToPlaylistSheet.show(ctx);
                },
                child: const Text('open'),
              ),
            ),
          );
        }),
      ),
    ));
    await t.tap(find.text('open'));
    await t.pumpAndSettle();
    await t.tap(find.byKey(const Key('add_to_playlist_p1')));
    await t.pumpAndSettle();
    expect(picked, 'p1');
  });
}
