import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/core/contracts/server.dart';
import 'package:smplayer/core/domain/ports/server_repository.dart';
import 'package:smplayer/core/domain/usecases/server_usecases.dart';

class _CapturingServerRepository implements ServerRepository {
  List<EmbyServer>? replacedServers;

  @override
  Future<void> replaceAll(List<EmbyServer> servers) async {
    replacedServers = List<EmbyServer>.of(servers);
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

void main() {
  group('ReorderServersUseCase', () {
    test('persists the provided server order using replaceAll', () async {
      final repository = _CapturingServerRepository();
      final usecase = ReorderServersUseCase(serverRepository: repository);
      final originalServers = <EmbyServer>[
        _server(id: 'first'),
        _server(id: 'second'),
        _server(id: 'third'),
      ];
      final reorderedServers = <EmbyServer>[
        originalServers[1],
        originalServers[2],
        originalServers[0],
      ];

      await usecase.execute(reorderedServers);

      expect(repository.replacedServers?.map((server) => server.id), [
        'second',
        'third',
        'first',
      ]);
    });
  });
}

EmbyServer _server({required String id}) {
  return EmbyServer(
    id: id,
    name: 'Server $id',
    baseUrl: 'https://$id.example.com/emby',
    createdAt: '2026-01-01T00:00:00.000Z',
    updatedAt: '2026-01-01T00:00:00.000Z',
  );
}
