<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.qtvoice.softphone">

    <!-- ========== PERMISSIONS ========== -->
    
    <!-- Microphone for VoIP calls (REQUIRED) -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    
    <!-- Network for VoIP -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    <!-- Push Notifications (REQUIRED for incoming calls) -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <!-- Background call handling -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_PHONE_CALL" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <!-- Incoming call UI when screen is off -->
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    
    <!-- Phone state for call management -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
    
    <!-- Bluetooth headset support -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <!-- ========== FEATURES ========== -->
    
    <uses-feature android:name="android.hardware.microphone" android:required="true" />
    <uses-feature android:name="android.hardware.telephony" android:required="false" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="false"
        android:networkSecurityConfig="@xml/network_security_config">

        <!-- Main Activity -->
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:exported="true"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:showWhenLocked="true"
            android:turnScreenOn="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Incoming Call Activity (shows over lock screen) -->
        <activity
            android:name=".IncomingCallActivity"
            android:exported="false"
            android:launchMode="singleTop"
            android:showWhenLocked="true"
            android:turnScreenOn="true"
            android:excludeFromRecents="true"
            android:taskAffinity=""
            android:theme="@style/Theme.IncomingCall" />

        <!-- ========== FCM SERVICES ========== -->
        
        <!-- FCM Push Notification Service -->
        <service
            android:name=".fcm.QtvoiceFcmService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <!-- ========== TWILIO VOICE SERVICES ========== -->
        
        <!-- Foreground Service for active calls -->
        <service
            android:name=".voice.VoiceCallService"
            android:exported="false"
            android:foregroundServiceType="phoneCall">
        </service>

        <!-- ConnectionService for system call UI integration -->
        <service
            android:name=".voice.VoiceConnectionService"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE"
            android:exported="true">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>

        <!-- ========== RECEIVERS ========== -->
        
        <!-- Boot receiver to re-register for push -->
        <receiver
            android:name=".receivers.BootReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <!-- Call action receiver (answer/reject from notification) -->
        <receiver
            android:name=".receivers.CallActionReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.qtvoice.softphone.ACTION_ANSWER" />
                <action android:name="com.qtvoice.softphone.ACTION_REJECT" />
                <action android:name="com.qtvoice.softphone.ACTION_HANGUP" />
            </intent-filter>
        </receiver>

        <!-- ========== META DATA ========== -->
        
        <!-- Default FCM channel -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="qtvoice_calls" />

        <!-- FCM auto-init -->
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="true" />

    </application>

</manifest>
