<?php
// auth.php — Updated with tracking

// Check if a session is already active before starting a new one
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
define('SESSION_TIMEOUT', 600);

if (isset($_SESSION['LAST_ACTIVITY']) && 
    (time() - $_SESSION['LAST_ACTIVITY'] > SESSION_TIMEOUT)) {
    
    // ثبت پایان سشن قبل از destroy
    if (isset($_SESSION['username'])) {
        require_once 'tracker.php';
        $tracker = new ActivityTracker();
        $tracker->endSession();
        $tracker->logAction($_SESSION['username'], 'session_timeout', 'Session expired after inactivity');
    }
    
    session_unset();
    session_destroy();
    header('Location: login.php?expired=1');
    exit();
}

$_SESSION['LAST_ACTIVITY'] = time();

function is_logged_in() {
    return isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true;
}

function check_access() {
    if (!is_logged_in()) {
        header('Location: login.php');
        exit();
    }
}

function get_user_access_config() {
    if (!is_logged_in()) return null;
    
    return [
        'role' => $_SESSION['user_role'] ?? 'viewer',
        'area_supervisor_id' => $_SESSION['area_supervisor_id'] ?? null,
        'area_supervisor_ids' => $_SESSION['area_supervisor_ids'] ?? null,
        'user_name' => $_SESSION['user_name'] ?? '',
    ];
}


// ===== تعریف کاربران =====

$valid_users = [
    'shayan' => [
        'password' => password_hash('2269880', PASSWORD_DEFAULT),
        'name' => 'شایان شیرمحمدی',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    'babakrahimi' => [
        'password' => password_hash('2646093', PASSWORD_DEFAULT),
        'name' => 'بابک رحیمی',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    'hammaslak' => [
        'password' => password_hash('09129579129', PASSWORD_DEFAULT),
        'name' => 'بهمن هم مسلک',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    'mahali' => [
        'password' => password_hash('9121911057', PASSWORD_DEFAULT),
        'name' => 'میثم محالی',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    '9122376824' => [
        'password' => password_hash('873641295', PASSWORD_DEFAULT),
        'name' => 'فتحی پور محمد',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    'khani' => [
        'password' => password_hash('9129623952', PASSWORD_DEFAULT),
        'name' => 'علیرضا خانی',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    'tabarzadi' => [
        'password' => password_hash('9123797591', PASSWORD_DEFAULT),
        'name' => 'محمد طبرزدی',
        'role' => 'admin',
        'area_supervisor_id' => null
    ],
    '9135898963' => [
        'password' => password_hash('293847561', PASSWORD_DEFAULT),
        'name' => 'حسینی نسب عماد',
        'role' => 'admin',
        'area_supervisor_id' => '9135898963'
    ],
    
    
   
   
   
   
    'faridnouri' => [
        'password' => password_hash('2938392561', PASSWORD_DEFAULT),
        'name' => 'فرید نوری',
        'role' => 'regional_manager',
        'area_supervisor_ids' => ['9910611056', '9124555839', '9386908813'] 
    ],
    
        'majidmohseni' => [
        'password' => password_hash('153683961', PASSWORD_DEFAULT),
        'name' => 'محسنی مجید',
        'role' => 'regional_manager',
        'area_supervisor_ids' => ['9392578949', '9166160144'] 
    ],
    
    
    
    
    '9148055603' => [
        'password' => password_hash('483927156', PASSWORD_DEFAULT),
        'name' => 'عزت شعار علی',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9148055603'
    ],
    '9122750312' => [
        'password' => password_hash('271564839', PASSWORD_DEFAULT),
        'name' => 'پوررجبی امید',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9122750312'
    ],
    '9153619979' => [
        'password' => password_hash('639174285', PASSWORD_DEFAULT),
        'name' => 'اسماعیلی محسن',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9153619979'
    ],
    '9124555839' => [
        'password' => password_hash('815492367', PASSWORD_DEFAULT),
        'name' => 'جمشیدی محمد',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9124555839'
    ],
    '9386908813' => [
        'password' => password_hash('394658172', PASSWORD_DEFAULT),
        'name' => 'دشت افکن مهدی',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9386908813'
    ],
    '9172129171' => [
        'password' => password_hash('726581943', PASSWORD_DEFAULT),
        'name' => 'بهادری مرتضی',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9172129171'
    ],
    '9921617658' => [
        'password' => password_hash('158374629', PASSWORD_DEFAULT),
        'name' => 'اتحاد مصطفی',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9921617658'
    ],
    '9120451332' => [
        'password' => password_hash('942738516', PASSWORD_DEFAULT),
        'name' => 'اسگندری حجت',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9120451332'
    ],
    '9392578949' => [
        'password' => password_hash('367219485', PASSWORD_DEFAULT),
        'name' => 'بختیاریان رضا',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9392578949'
    ],
    '9153034725' => [
        'password' => password_hash('584196273', PASSWORD_DEFAULT),
        'name' => 'کلالی مهیار',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9153034725'
    ],
    '9120991439' => [
        'password' => password_hash('429837165', PASSWORD_DEFAULT),
        'name' => 'ابراهیمی امیر',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9120991439'
    ],
    '9363076600' => [
        'password' => password_hash('751628394', PASSWORD_DEFAULT),
        'name' => 'پاشایی بهنام',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9363076600'
    ],
    '9120259531' => [
        'password' => password_hash('475938261', PASSWORD_DEFAULT),
        'name' => 'پور اسد محسن',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9120259531'
    ],
    '9188594147' => [
        'password' => password_hash('836215749', PASSWORD_DEFAULT),
        'name' => 'مرزبان یزدان',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9188594147'
    ],
    '9120375750' => [
        'password' => password_hash('159482637', PASSWORD_DEFAULT),
        'name' => 'عابد مجتبی',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9120375750'
    ],
    '9355732174' => [
        'password' => password_hash('682317495', PASSWORD_DEFAULT),
        'name' => 'بهنان مسعود',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9355732174'
    ],
    '9129382093' => [
        'password' => password_hash('347589162', PASSWORD_DEFAULT),
        'name' => 'قدرتی میلاد',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9129382093'
    ],
    '9039404332' => [
        'password' => password_hash('519263847', PASSWORD_DEFAULT),
        'name' => 'منجزی وند رضا',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9039404332'
    ],
    '9160968543' => [
        'password' => password_hash('264918375', PASSWORD_DEFAULT),
        'name' => 'علی محمدی ساسان',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9160968543'
    ],
    '9910611056' => [
        'password' => password_hash('495172638', PASSWORD_DEFAULT),
        'name' => 'جعفری رضا',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9910611056'
    ],
    '9144486557' => [
        'password' => password_hash('738261594', PASSWORD_DEFAULT),
        'name' => 'وجدان رضا',
        'role' => 'area_supervisor',
        'area_supervisor_id' => '9144486557'
    ],

];

function login($username, $password) {
    global $valid_users;
    
    if (isset($valid_users[$username])) {
        if (password_verify($password, $valid_users[$username]['password'])) {
            $_SESSION['logged_in'] = true;
            $_SESSION['username'] = $username;
            $_SESSION['user_name'] = $valid_users[$username]['name'];
            $_SESSION['user_role'] = $valid_users[$username]['role'];
            $_SESSION['area_supervisor_id'] = $valid_users[$username]['area_supervisor_id'] ?? null;
            $_SESSION['area_supervisor_ids'] = $valid_users[$username]['area_supervisor_ids'] ?? null;
            $_SESSION['login_time'] = time();
            
            // ثبت لاگین در ترکر
            require_once 'tracker.php';
            $tracker = new ActivityTracker();
            $tracker->startSession($username, $valid_users[$username]['role']);
            $tracker->logAction($username, 'login', 'User logged in');
            
            return true;
        }
    }
    return false;
}

function logout() {
    // ثبت لاگ‌اوت در ترکر
    if (isset($_SESSION['username'])) {
        require_once 'tracker.php';
        $tracker = new ActivityTracker();
        $tracker->logAction($_SESSION['username'], 'logout', 'User logged out');
        $tracker->endSession();
    }
    
    $_SESSION = array();
    session_destroy();
}
?>
