import 'package:permission_handler/permission_handler.dart';

class LocationPermissionService {
  Future<LocationPermissionResult> requestLocationPermission() async {
    final status = await Permission.location.request();

    return switch (status) {
      PermissionStatus.granted => LocationPermissionResult.granted(),
      PermissionStatus.denied => LocationPermissionResult.denied(),
      PermissionStatus.permanentlyDenied =>
        LocationPermissionResult.permanentlyDenied(),
      _ => LocationPermissionResult.error(
          message: 'Unexpected permission status: $status',
        ),
    };
  }

  Future<bool> hasLocationPermission() async {
    final status = await Permission.location.status;
    return status.isGranted;
  }

  Future<void> openAppSettings() async {
    await openAppSettings();
  }
}

sealed class LocationPermissionResult {
  const LocationPermissionResult();

  factory LocationPermissionResult.granted() => _Granted();
  factory LocationPermissionResult.denied() => _Denied();
  factory LocationPermissionResult.permanentlyDenied() =>
      _PermanentlyDenied();
  factory LocationPermissionResult.error({required String message}) =>
      _Error(message);

  R when<R>({
    required R Function() granted,
    required R Function() denied,
    required R Function() permanentlyDenied,
    required R Function(String message) error,
  }) {
    return switch (this) {
      _Granted() => granted(),
      _Denied() => denied(),
      _PermanentlyDenied() => permanentlyDenied(),
      _Error(:final message) => error(message),
    };
  }
}

final class _Granted extends LocationPermissionResult {
  const _Granted();
}

final class _Denied extends LocationPermissionResult {
  const _Denied();
}

final class _PermanentlyDenied extends LocationPermissionResult {
  const _PermanentlyDenied();
}

final class _Error extends LocationPermissionResult {
  final String message;
  const _Error(this.message);
}
