import 'package:flutter_test/flutter_test.dart';
import 'package:linthra/core/diagnostics/safe_event_log.dart';
import 'package:linthra/core/sources/jellyfin/jellyfin_api.dart';
import 'package:linthra/core/sources/jellyfin/jellyfin_sync_diagnostics.dart';

void main() {
  group('JellyfinSyncDiagnostics.skipped', () {
    setUp(SafeEventLog.instance.clear);
    tearDown(SafeEventLog.instance.clear);

    test('records a secret-free breadcrumb when items were skipped', () {
      JellyfinSyncDiagnostics.skipped(
        kind: JellyfinItemKind.audio,
        skipped: 2,
        kept: 40,
      );

      expect(SafeEventLog.instance.lines, hasLength(1));
      final String line = SafeEventLog.instance.lines.single;
      expect(line, contains('jellyfin-sync'));
      expect(line, contains('skip:audio'));
      expect(line, contains('dropped=2'));
      expect(line, contains('kept=40'));
    });

    test('is silent when nothing was skipped', () {
      JellyfinSyncDiagnostics.skipped(
        kind: JellyfinItemKind.album,
        skipped: 0,
        kept: 12,
      );

      expect(SafeEventLog.instance.isEmpty, isTrue);
    });

    test('carries only the kind name and counts — never free text', () {
      // The API has no parameter for a title, URL, or token; this guards that
      // the recorded detail is structural only.
      JellyfinSyncDiagnostics.skipped(
        kind: JellyfinItemKind.artist,
        skipped: 1,
        kept: 0,
      );

      final String line = SafeEventLog.instance.lines.single;
      expect(line, 'jellyfin-sync: skip:artist dropped=1 kept=0');
    });
  });

  group('JellyfinSyncDiagnostics.failure', () {
    setUp(SafeEventLog.instance.clear);
    tearDown(SafeEventLog.instance.clear);

    test('records category, status, action, and probe outcome', () {
      JellyfinSyncDiagnostics.failure(
        category: 'notReachable',
        action: 'library',
        statusCode: null,
        reachable: true,
        authOk: true,
      );

      final String line = SafeEventLog.instance.lines.single;
      expect(
        line,
        'jellyfin-sync: fail:library category=notReachable status=- '
        'reachable=yes auth=yes',
      );
    });

    test('renders unknown probe results as "unknown" and a status code', () {
      JellyfinSyncDiagnostics.failure(
        category: 'serverError',
        action: 'library',
        statusCode: 503,
      );

      final String line = SafeEventLog.instance.lines.single;
      expect(line, contains('status=503'));
      expect(line, contains('reachable=unknown'));
      expect(line, contains('auth=unknown'));
    });
  });
}
