<?php
declare(strict_types=1);

date_default_timezone_set('Europe/Athens');

$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ((int)($_SERVER['SERVER_PORT'] ?? 0) === 443);

if (session_status() === PHP_SESSION_NONE) {
    session_set_cookie_params([
        'lifetime' => 0,
        'path' => '/',
        'secure' => $https,
        'httponly' => true,
        'samesite' => 'Lax',
    ]);
    session_start();
}

require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../helpers/common.php';
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/project_permissions.php';
require_once __DIR__ . '/../helpers/weather_helper.php';

// Autoload (or manual include) models
require_once __DIR__ . '/../models/ProjectModel.php';
require_once __DIR__ . '/../models/CustomerModel.php';

function gp_date($d, $withTime = null) {
    if (empty($d) || $d == '0000-00-00' || $d == '0000-00-00 00:00:00') return '-';
    $time = strtotime($d);
    if (!$time) return '-';
    
    // If withTime is explicitly set, use it. Otherwise, auto-detect from ':' in string.
    $showTime = ($withTime === true) || ($withTime === null && strpos($d, ':') !== false);
    
    if ($showTime) {
        return date('d/m/Y H:i', $time);
    }
    return date('d/m/Y', $time);
}
