<IfModule mod_headers.c>
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, X-App-Name"
</IfModule>

# Return 200 for all OPTIONS preflight requests immediately
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^ - [R=200,L]

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /api/

    RewriteRule ^v1/subscription/status/?$       v1/check_subscription.php   [L,QSA]
    RewriteRule ^v1/otp/request/?$                v1/send_otp.php             [L,QSA]
    RewriteRule ^v1/otp/verify/?$                 v1/verify_otp.php           [L,QSA]
    RewriteRule ^v1/subscription/unsubscribe/?$   v1/unsubscribe.php          [L,QSA]

    # USSD stays as two separate files, same as the rest of this backend:
    #  - ussd/receive is the inbound-MO webhook bdapps calls directly
    #  - ussd/send is your own admin/internal trigger endpoint
    RewriteRule ^v1/ussd/receive/?$               v1/ussd_receive.php         [L,QSA]
    RewriteRule ^v1/ussd/send/?$                   v1/ussd_send.php            [L,QSA]

    # bKash Charging SDK
    RewriteRule ^v1/bkash/subscribe/?$            v1/bkash_subscribe.php      [L,QSA]
    RewriteRule ^v1/bkash/return/?$                v1/bkash_return.php         [L,QSA]
</IfModule>

# Block direct access to sensitive files.
# Everything — BDApps app_id/password AND bKash api_key/api_secret — lives in
# the one app_credentials.php file, so that's the one that must never be
# directly requestable. app_auth.php is middleware, not secrets, but it's
# blocked too since it's never meant to be hit as a URL on its own.
<FilesMatch "^(app_credentials|app_auth)\.php$">
    Require all denied
    # For older Apache (2.2), use instead:
    # Order allow,deny
    # Deny from all
</FilesMatch>

# Also block direct access to log files as defense-in-depth
<FilesMatch "\.(txt|log)$">
    Require all denied
</FilesMatch>
