import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart' as permission;

class SMSService {
  static const platform = MethodChannel('com.submanager/sms');

  static Future<bool> requestSMSPermission() async {
    if (kIsWeb) return false;
    final status = await permission.Permission.sms.request();
    return status.isGranted;
  }

  static Future<bool> hasSMSPermission() async {
    if (kIsWeb) return false;
    final status = await permission.Permission.sms.status;
    return status.isGranted;
  }

  static Future<List<Map<String, dynamic>>> scanSubscriptions() async {
    if (kIsWeb) return [];

    try {
      if (!await hasSMSPermission()) {
        throw Exception('SMS 권한이 없습니다.');
      }

      final List<dynamic> result =
          await platform.invokeMethod('scanSubscriptions');
      return result.map((item) => item as Map<String, dynamic>).toList();
    } on PlatformException catch (e) {
      throw Exception('SMS 스캔 중 오류 발생: ${e.message}');
    }
  }
}
