RewriteEngine On
Options -Indexes
Options -MultiViews

# Turn on rewrite logs if you need to debug (check server config for RewriteLogLevel)
# RewriteLog "/path/to/your/rewrite.log"
# RewriteLogLevel 3

# Redirect non-www to www (optional, uncomment to use)
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Basic security: Deny access to .htaccess, .htpasswd, config files, etc.
<FilesMatch "^\.(htaccess|htpasswd)$|config\.php|error_log|composer\.(json|lock)|package(-lock)?\.json|README\.md|LICENSE|phpunit\.xml">
    Require all denied
</FilesMatch>

# Deny directory listing
# Options -Indexes (already set above, but good to be aware)

# === Core Pages ===
# index
RewriteRule index index.php [L,QSA]

# Dashboard (Top Level - if used)
RewriteRule dashboard dashboard.php [L,QSA]

# === Admin Section ===
# Admin Dashboard (Specific rule for header.php link)
RewriteRule ^admin/dashboard$ admin/dashboard.php [L,QSA]

# วัสดุ (Material)
RewriteRule ^admin/materials$ admin/materials_list.php [L,QSA]
RewriteRule ^admin/material/add$ admin/material_add.php [L,QSA]
RewriteRule ^admin/material/edit/([0-9]+)$ admin/material_edit.php?id=$1 [L,QSA]
RewriteRule ^admin/material/delete/([0-9]+)$ admin/material_delete.php?id=$1 [L,QSA]

# ครุภัณฑ์ (Equipment)
RewriteRule ^admin/equipment$ admin/equipment_list.php [L,QSA]
RewriteRule ^admin/equipment/add$ admin/equipment_add.php [L,QSA]
RewriteRule ^admin/equipment/edit/([0-9]+)$ admin/equipment_edit.php?id=$1 [L,QSA]
RewriteRule ^admin/equipment/delete/([0-9]+)$ admin/equipment_delete.php?id=$1 [L,QSA]

# ประเภทครุภัณฑ์ (Equipment Categories)
RewriteRule ^admin/equipment-categories$ admin/equipment_categories_list.php [L,QSA]
RewriteRule ^admin/equipment-category/add$ admin/equipment_category_add.php [L,QSA]
RewriteRule ^admin/equipment-category/edit/([0-9]+)$ admin/equipment_category_edit.php?id=$1 [L,QSA]
RewriteRule ^admin/equipment-category/delete/([0-9]+)$ admin/equipment_category_delete.php?id=$1 [L,QSA]

# หน่วยนับ (Units)
RewriteRule ^admin/units$ admin/units_list.php [L,QSA]
RewriteRule ^admin/unit/add$ admin/unit_add.php [L,QSA]
RewriteRule ^admin/unit/edit/([0-9]+)$ admin/unit_edit.php?id=$1 [L,QSA]
RewriteRule ^admin/unit/delete/([0-9]+)$ admin/unit_delete.php?id=$1 [L,QSA]

# หน่วยงาน (Departments)
RewriteRule ^admin/departments$ admin/departments_list.php [L,QSA]
RewriteRule ^admin/department/add$ admin/department_add.php [L,QSA]
RewriteRule ^admin/department/edit/([0-9]+)$ admin/department_edit.php?id=$1 [L,QSA]
RewriteRule ^admin/department/delete/([0-9]+)$ admin/department_delete.php?id=$1 [L,QSA]

# ผู้ใช้ (Users)
RewriteRule ^admin/users$ admin/users_list.php [L,QSA]
RewriteRule ^admin/user/add$ admin/user_add.php [L,QSA]
RewriteRule ^admin/user/edit/([0-9]+)$ admin/user_edit.php?id=$1 [L,QSA]
RewriteRule ^admin/user/delete/([0-9]+)$ actions/user_delete_action.php?id=$1 [L,QSA] # Note: target is actions/

# อนุมัติและสต็อก (Approval and Stock)
RewriteRule ^admin/stock_management$ admin/stock_management.php [L,QSA]
RewriteRule ^admin/material_requests_approval$ admin/material_requests_approval.php [L,QSA]
RewriteRule ^admin/equipment_loans_approval$ admin/equipment_loans_approval.php [L,QSA]
RewriteRule ^admin/maintenance_requests_list$ admin/maintenance_requests_list.php [L,QSA]

# รายงาน (Reports)
RewriteRule ^admin/report/equipment/stock$ admin/report_equipment_stock.php [L,QSA]
RewriteRule ^admin/report/material/rejected$ admin/report_material_rejected.php [L,QSA]
RewriteRule ^admin/report/equipment/loan-cancelled-rejected$ admin/report_equipment_loan_cancelled_rejected.php [L,QSA]
RewriteRule ^admin/report/material/stock$ admin/report_material_stock.php [L,QSA]
RewriteRule ^admin/report/material/disbursement$ admin/report_material_disbursement.php [L,QSA]
RewriteRule ^admin/report/equipment/loan-history$ admin/report_equipment_loan_history.php [L,QSA]
RewriteRule ^admin/report/equipment/status$ admin/report_equipment_status.php [L,QSA]
RewriteRule ^admin/report/maintenance/requests$ admin/report_maintenance_requests.php [L,QSA]

# รายการเบิกวัสดุ (Material Requests List)
RewriteRule ^admin/material-requests$ admin/material_requests_list.php [L,QSA]
RewriteRule ^admin/material-request/view/([0-9]+)$ admin/material_request_view.php?id=$1 [L,QSA]

# รายการยืมครุภัณฑ์ (Equipment Loans List)
RewriteRule ^admin/equipment-loans$ admin/equipment_loans_list.php [L,QSA]
RewriteRule ^admin/equipment-loan/view/([0-9]+)$ admin/equipment_loan_view.php?id=$1 [L,QSA]

# === User Section ===
# เมนู user (Pretty URL)
RewriteRule ^user/material/request$ user/create_material_request.php [L,QSA]
RewriteRule ^user/material/request/history$ user/material_request_history.php [L,QSA]
RewriteRule ^user/equipment/loan$ user/create_equipment_loan.php [L,QSA]
RewriteRule ^user/equipment/loan/history$ user/equipment_loan_history.php [L,QSA]
RewriteRule ^user/maintenance/request$ user/create_maintenance_request.php [L,QSA]
RewriteRule ^user/maintenance/request/history$ user/maintenance_request_history.php [L,QSA]

# === Authentication ===
RewriteRule ^login$ login.php [L,QSA]
RewriteRule ^logout$ logout.php [L,QSA]
RewriteRule ^sso-login$ sso_login.php [L,QSA]

# === Default fallback for admin pages (OPTIONAL) ===
# This rule, if uncommented, would handle any admin URL not caught by specific rules above.
# e.g., admin/somepage would be rewritten to admin/somepage.php.
# The conditions ensure it doesn't interfere with:
#   1. Requests for existing files (-f) or directories (-d).
#   2. URLs that already end in .php (like the recovery page links in header.php).
# To use it, uncomment the following 4 lines and ensure it's placed AFTER specific admin rules.
#
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !\.php$
# RewriteRule ^admin/(.+)$ admin/$1.php [L,QSA]
# The comment "ปรับปรุงให้ตรงกับไฟล์ header" (Update to match the header file) means ensuring this generic
# rule is compatible with the types of URLs generated by header.php (both pretty ones like 'admin/users'
# and direct .php links like 'admin/users_deleted_list.php'). The conditions help achieve this.

# === Fallback for non-existent files/directories (optional: redirect to index or 404) ===
# If you want to redirect all other non-existent paths to index.php:
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . index.php [L]

# If you want to use a custom 404 page:
ErrorDocument 404 /error_pages/404.php
