RewriteEngine On
RewriteBase /

# --- CORS HEADERS FOR PHP APIs ---
<IfModule mod_headers.c>
    # PHP files ke liye CORS headers
    <FilesMatch "\.(php)$">
        Header always set Access-Control-Allow-Origin "https://stories.funtelstudio.com"
        Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
        Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With, Accept, Origin, X-CSRF-Token"
        Header always set Access-Control-Allow-Credentials "true"
        Header always set Access-Control-Max-Age "3600"
    </FilesMatch>
</IfModule>

# --- 1) Admin panel ke liye alag rule ---
RewriteCond %{REQUEST_URI} ^/funadmin/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^funadmin/.*$ /funadmin/index.html [L]

# --- 2) API folder ko exclude karo ---
RewriteCond %{REQUEST_URI} ^/storyapi/ [NC]
RewriteRule ^ - [L]

# --- 3) CORS preflight handle ---
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

# --- 4) Main Angular app ke liye ---
RewriteCond %{REQUEST_URI} !^/funadmin/ [NC]
RewriteCond %{REQUEST_URI} !\.(php|html|css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|map)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.html [L]

# --- 5) Cache rules ---
<FilesMatch "\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>

<FilesMatch "\.(html)$">
    Header set Cache-Control "no-store, no-cache, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
</FilesMatch>