import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/api/marianumcloud/capabilities/nextcloud_sharing_capabilities.dart';

void main() {
  group('NextcloudSharingCapabilities.fromFilesSharing', () {
    test('parses a full files_sharing block', () {
      final caps = NextcloudSharingCapabilities.fromFilesSharing({
        'api_enabled': true,
        'public': {
          'enabled': true,
          'multiple_links': true,
          'upload': true,
          'password': {'enforced': true},
          'expire_date': {'enabled': true, 'days': 7, 'enforced': true},
        },
        'group': {'enabled': true},
        'resharing': true,
      });
      expect(caps.apiEnabled, isTrue);
      expect(caps.publicEnabled, isTrue);
      expect(caps.publicMultipleLinks, isTrue);
      expect(caps.publicUploadEnabled, isTrue);
      expect(caps.publicPasswordEnforced, isTrue);
      expect(caps.publicExpireEnabled, isTrue);
      expect(caps.publicExpireDays, 7);
      expect(caps.publicExpireEnforced, isTrue);
      expect(caps.groupEnabled, isTrue);
      expect(caps.resharing, isTrue);
    });

    test('falls back to the legacy group_sharing flag', () {
      final caps = NextcloudSharingCapabilities.fromFilesSharing({
        'api_enabled': true,
        'group_sharing': true,
      });
      expect(caps.groupEnabled, isTrue);
    });

    test('missing fields default to the restrictive value', () {
      final caps = NextcloudSharingCapabilities.fromFilesSharing({});
      expect(caps.apiEnabled, isFalse);
      expect(caps.publicEnabled, isFalse);
      expect(caps.groupEnabled, isFalse);
      expect(caps.resharing, isFalse);
      expect(caps.publicExpireDays, isNull);
    });

    test('tolerates non-map / wrong-typed nested values', () {
      final caps = NextcloudSharingCapabilities.fromFilesSharing({
        'api_enabled': 'yes', // not a bool -> false
        'public': 'nope', // not a map -> all false
      });
      expect(caps.apiEnabled, isFalse);
      expect(caps.publicEnabled, isFalse);
    });
  });
}
