import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
// ignore: depend_on_referenced_packages
import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart';

import 'package:recording_tool/core/permission/permission_service.dart';

void main() {
  group('PermissionService.requestMissing', () {
    late PermissionHandlerPlatform originalPlatform;

    setUp(() {
      originalPlatform = PermissionHandlerPlatform.instance;
    });

    tearDown(() {
      PermissionHandlerPlatform.instance = originalPlatform;
    });

    test('requests only missing permissions and skips granted ones', () async {
      final platform = FakePermissionHandlerPlatform(
        statuses: <Permission, PermissionStatus>{
          Permission.camera: PermissionStatus.granted,
          Permission.microphone: PermissionStatus.denied,
        },
        requestResults: <Permission, PermissionStatus>{
          Permission.microphone: PermissionStatus.granted,
        },
      );
      PermissionHandlerPlatform.instance = platform;

      final result = await PermissionService.requestMissing(<Permission>[
        Permission.camera,
        Permission.microphone,
      ]);

      expect(platform.requestCalls, <List<Permission>>[
        <Permission>[Permission.microphone],
      ]);
      expect(result[Permission.camera], PermissionStatus.granted);
      expect(result[Permission.microphone], PermissionStatus.granted);
    });

    test(
      'preserves permanently denied permissions without requesting them',
      () async {
        final platform = FakePermissionHandlerPlatform(
          statuses: <Permission, PermissionStatus>{
            Permission.camera: PermissionStatus.permanentlyDenied,
            Permission.microphone: PermissionStatus.denied,
          },
          requestResults: <Permission, PermissionStatus>{
            Permission.microphone: PermissionStatus.granted,
          },
        );
        PermissionHandlerPlatform.instance = platform;

        final result = await PermissionService.requestMissing(<Permission>[
          Permission.camera,
          Permission.microphone,
        ]);

        expect(platform.requestCalls, <List<Permission>>[
          <Permission>[Permission.microphone],
        ]);
        expect(result[Permission.camera], PermissionStatus.permanentlyDenied);
        expect(result[Permission.microphone], PermissionStatus.granted);
      },
    );
  });

  group('iOS permission configuration', () {
    test('Podfile enables camera and microphone permission macros only', () {
      final podfile = File('ios/Podfile').readAsStringSync();

      expect(
        podfile,
        contains('flutter_additional_ios_build_settings(target)'),
      );
      expect(podfile, contains("'PERMISSION_CAMERA=1'"));
      expect(podfile, contains("'PERMISSION_MICROPHONE=1'"));
      expect(podfile, isNot(contains("'PERMISSION_PHOTOS=1'")));
      expect(podfile, isNot(contains("'PERMISSION_PHOTOS_ADD_ONLY=1'")));
    });
  });
}

class FakePermissionHandlerPlatform extends PermissionHandlerPlatform {
  FakePermissionHandlerPlatform({
    required this.statuses,
    required this.requestResults,
  });

  final Map<Permission, PermissionStatus> statuses;
  final Map<Permission, PermissionStatus> requestResults;
  final List<List<Permission>> requestCalls = <List<Permission>>[];

  @override
  Future<PermissionStatus> checkPermissionStatus(Permission permission) async {
    return statuses[permission] ?? PermissionStatus.denied;
  }

  @override
  Future<ServiceStatus> checkServiceStatus(Permission permission) async {
    return ServiceStatus.enabled;
  }

  @override
  Future<bool> openAppSettings() async {
    return true;
  }

  @override
  Future<Map<Permission, PermissionStatus>> requestPermissions(
    List<Permission> permissions,
  ) async {
    requestCalls.add(List<Permission>.unmodifiable(permissions));
    return <Permission, PermissionStatus>{
      for (final permission in permissions)
        permission: requestResults[permission] ?? PermissionStatus.granted,
    };
  }

  @override
  Future<bool> shouldShowRequestPermissionRationale(
    Permission permission,
  ) async {
    return false;
  }
}
