# Włącz moduł rewrite
RewriteEngine On

# Wyświetlanie błędów PHP tylko dla mod_php
<IfModule mod_php.c>
    php_flag display_errors On
    php_value error_reporting E_ALL
</IfModule>

# Ustaw domyślną stronę kodowania
AddDefaultCharset UTF-8

# Blokada dostępu do plików wrażliwych
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|sql)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order Allow,Deny
        Deny from all
    </IfModule>
</FilesMatch>

# Przekierowanie z www na bez www (opcjonalne)
# RewriteCond %{HTTP_HOST} ^www\.togethere\.cloud$ [NC]
# RewriteRule ^(.*)$ https://togethere.cloud/$1 [R=301,L]

# Wymuszenie HTTPS (odkomentuj gdy będzie certyfikat SSL)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Ping-Pong 1v1 WebSocket proxy do lokalnego serwera Node.js
<IfModule mod_proxy.c>
    RewriteCond %{REQUEST_URI} ^/ping-pong-1v1/?$ [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule ^ping-pong-1v1/?$ ws://127.0.0.1:8088/ [P,L]

    RewriteRule ^ping-pong-1v1/health$ http://127.0.0.1:8088/health [P,L]
</IfModule>

# Usuwanie .php z URL - przekierowanie 301 (tylko dla GET, nie dla POST)
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# Przekierowanie na folder z trailing slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.+)$ /$1/ [R=301,L]

# Automatyczne przekierowanie na index.php w folderze
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.+)/$ $1/index.php [L]

# Dodawanie .php do plików (wewnętrznie) - tylko jeśli nie jest folderem
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

# Strony błędów (opcjonalne)
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

# Kompresja GZIP
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Cachowanie
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
</IfModule>
