<?php
/**
 * Get Rules API
 * 
 * Returns rules and regulations in a localized format (Urdu or English).
 */
// Addressing PHPSESSID SameSite rejection
session_set_cookie_params([
    'lifetime' => 0,
    'path' => '/',
    'domain' => '',
    'secure' => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on',
    'httponly' => true,
    'samesite' => 'Lax'
]);
session_start();
require_once('../libs/classes/DbPathsArray.php');
require_once('../libs/classes/App_DB.php');

$crud = new App_DB(DBU::$dba_user);

$lang = $_GET['lang'] ?? $_COOKIE['lang'] ?? $_SESSION['lang'] ?? 'ur';
$field = ($lang === 'en') ? 'contents_en' : 'contents_ur';

$sql = "SELECT rule_no, $field as content FROM rules_and_regulation ORDER BY rule_no ASC";
$rules = $crud->getRecordSet($sql);

if (!empty($rules)) {
    echo '<table id="rulesTable" class="table table-borderless mt-4" style="direction: ' . ($lang === 'en' ? 'ltr' : 'rtl') . ';">';
    foreach ($rules as $rule) {
        echo '<tr>';
        echo '<td width="50" class="fw-bold">' . $rule['rule_no'] . ')</td>';
        echo '<td class="rule-content">' . htmlspecialchars($rule['content']) . '</td>';
        echo '</tr>';
    }
    echo '</table>';
} else {
    echo '<div class="alert alert-info">No rules found.</div>';
}
?>
