# Disable directory listing
Options -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Set base path – adjust for local or live
    # Local:   RewriteBase /talent/
    # Live:    RewriteBase /
    RewriteBase /talent/

    # ─── APPLICATION DETAILS CLEAN URL ───
    RewriteRule ^applicant/application-details/([0-9]+)/?$ applicant/application-details.php?id=$1 [L,QSA]
    RewriteRule ^applicant/invoice/([0-9]+)/?$ applicant/invoice.php?id=$1 [L,QSA]
    RewriteRule ^applicant/payment/([0-9]+)/?$ applicant/payment.php?id=$1 [L,QSA]
    RewriteRule ^applicant/support-view/([0-9]+)/?$ applicant/support-view.php?id=$1 [L,QSA]

    ####################################################
    # EXCLUDE /ajax/ FOLDER FROM REWRITE
    # This ensures .php files inside /ajax/ are served directly
    ####################################################
    RewriteRule ^ajax/ - [L]

    ####################################################
    # 301 Redirect .php URLs to clean URLs
    # Example: /about.php → /about
    ####################################################
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^(.+?)\.php$ /$1 [R=301,L]

    ####################################################
    # Internally rewrite clean URLs to .php files
    # (Only if the .php file exists)
    ####################################################
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]

    ####################################################
    # Handle admin and applicant sub‑directories
    ####################################################
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/(admin|applicant)/(.+)$
    RewriteCond %{DOCUMENT_ROOT}/%1/%2.php -f
    RewriteRule ^(admin|applicant)/(.+?)/?$ $1/$2.php [L]

</IfModule>

# ─── CUSTOM ERROR PAGES ───
ErrorDocument 403 /403
ErrorDocument 404 /404
ErrorDocument 500 /500


# ─── MAINTENANCE MODE (Optional - uncomment when needed) ───
# RewriteCond %{REQUEST_URI} !^/503\.php$
# RewriteCond %{REQUEST_URI} !^/admin/
# RewriteCond %{REQUEST_URI} !^/ajax/
# RewriteCond %{REQUEST_URI} !^/assets/
# RewriteCond %{REQUEST_URI} !^/css/
# RewriteCond %{REQUEST_URI} !^/js/
# RewriteCond %{REQUEST_URI} !^/images/
# RewriteRule ^(.*)$ /503.php [L]