<IfModule mod_rewrite.c>
    RewriteEngine On

    # 301 Canonical Redirects for direct legacy /views/ requests only
    RewriteCond %{THE_REQUEST} \s/views/explore/?[\s?] [NC]
    RewriteRule ^views/explore/?$ explore [R=301,L]

    RewriteCond %{THE_REQUEST} \s/views/dashboard/?[\s?] [NC]
    RewriteRule ^views/dashboard/?$ dashboard [R=301,L]

    RewriteCond %{THE_REQUEST} \s/views/my-ads/?[\s?] [NC]
    RewriteRule ^views/my-ads/?$ my-ads [R=301,L]

    RewriteCond %{THE_REQUEST} \s/views/studio/?[\s?] [NC]
    RewriteRule ^views/studio/?$ studio [R=301,L]

    RewriteCond %{THE_REQUEST} \s/views/wallet/?[\s?] [NC]
    RewriteRule ^views/wallet/?$ wallet [R=301,L]

    RewriteCond %{THE_REQUEST} \s/views/page\.php[\s?] [NC]
    RewriteRule ^views/page\.php$ privacy-policy [R=301,L]

    # Allow existing files and directories (like uploads/, assets/, admin/)
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Admin Panel Clean Routes
    RewriteRule ^admin/?$ admin/index.php?tab=dashboard [L,QSA]
    RewriteRule ^admin/([a-zA-Z0-9_-]+)/?$ admin/index.php?tab=$1 [L,QSA]

    # Custom Production Error Page & Health Routes
    ErrorDocument 404 404.php
    RewriteRule ^health/?$ health.php [L]

    # SEO Dynamic Engine Routes (Sitemap & Robots.txt)
    RewriteRule ^sitemap\.xml$ sitemap.php [L]
    RewriteRule ^robots\.txt$ robots.php [L]

    # Blog Clean Routes (e.g. /blog or /blog/double-whatsapp-ad-ctr-guide-2026)
    RewriteRule ^blog/([a-zA-Z0-9_-]+)/?$ index.php?view=blog_single&slug=$1 [L,QSA]
    RewriteRule ^blog/?$ index.php?view=blog_list [L,QSA]

    # Ad Detail View Route (e.g. /ad/12)
    RewriteRule ^ad/([0-9]+)/?$ index.php?adId=$1 [L,QSA]

    # App Sections Clean Routes (e.g. /explore, /dashboard, /my-ads, /studio, /wallet)
    RewriteRule ^explore/?$ index.php [L,QSA]
    RewriteRule ^dashboard/?$ index.php?view=dashboard [L,QSA]
    RewriteRule ^my-ads/?$ index.php?view=my_ads [L,QSA]
    RewriteRule ^studio/?$ index.php?openStudio=1 [L,QSA]
    RewriteRule ^wallet/?$ index.php?openWallet=1 [L,QSA]

    # Canonical Redirect direct *.php auth requests to clean URLs
    RewriteCond %{THE_REQUEST} \s/(?:auth/)?login\.php[\s?] [NC]
    RewriteRule ^ login [R=301,L]

    RewriteCond %{THE_REQUEST} \s/(?:auth/)?signup\.php[\s?] [NC]
    RewriteRule ^ signup [R=301,L]

    # Auth Clean Routes (e.g. /login, /signup, /logout, /google-login)
    RewriteRule ^login/?$ auth/login.php [L,QSA]
    RewriteRule ^signup/?$ auth/signup.php [L,QSA]
    RewriteRule ^register/?$ auth/signup.php [L,QSA]
    RewriteRule ^logout/?$ auth/logout.php [L,QSA]
    RewriteRule ^google-login/?$ auth/google_login.php [L,QSA]

    # Legal & Public Information Pages (e.g. /privacy-policy, /terms, /refund-policy, /about-us, /contact)
    RewriteRule ^privacy-policy/?$ views/page.php?slug=privacy-policy [L]
    RewriteRule ^terms/?$ views/page.php?slug=terms [L]
    RewriteRule ^terms-of-service/?$ views/page.php?slug=terms [L]
    RewriteRule ^refund-policy/?$ views/page.php?slug=refund-policy [L]
    RewriteRule ^about-us/?$ views/page.php?slug=about-us [L]
    RewriteRule ^contact/?$ views/page.php?slug=contact-us [L]
    RewriteRule ^contact-us/?$ views/page.php?slug=contact-us [L]
    RewriteRule ^page/([a-zA-Z0-9_-]+)/?$ views/page.php?slug=$1 [L]

    # Dynamic CMS Pages Catch-all Fallback
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9_-]+)/?$ views/page.php?slug=$1 [L,QSA]
</IfModule>

<IfModule mod_headers.c>
    # Prevent browser & proxy caching for dynamic PHP scripts and API endpoints
    <FilesMatch "\.(php)$">
        Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
        Header set Pragma "no-cache"
        Header set Expires "0"
    </FilesMatch>
</IfModule>
