<?php
/**
 * AURA FARMING — Configurações Globais
 * Etapa 21: progressão, proteção por idade, câmera segura, painel do responsável e privacidade.
 */

// É possível criar config/config.local.php para sobrescrever valores no servidor.
$localConfig = __DIR__ . '/config.local.php';
if (is_file($localConfig)) {
    require_once $localConfig;
}

function af_env(string $key, mixed $default = null): mixed
{
    $value = getenv($key);
    return ($value === false || $value === '') ? $default : $value;
}

// Mantém PHP e MySQL usando o mesmo relógio durante as partidas.
if (!defined('APP_TIMEZONE')) define('APP_TIMEZONE', (string)af_env('APP_TIMEZONE', 'America/Sao_Paulo'));
if (!@date_default_timezone_set(APP_TIMEZONE)) {
    date_default_timezone_set('UTC');
}

if (!defined('APP_ENV'))   define('APP_ENV', (string)af_env('APP_ENV', 'development'));
if (!defined('APP_DEBUG')) define('APP_DEBUG', filter_var(af_env('APP_DEBUG', APP_ENV !== 'production'), FILTER_VALIDATE_BOOL));

if (!defined('SITE_URL'))  define('SITE_URL', rtrim((string)af_env('SITE_URL', 'http://localhost/aura-farming'), '/'));
if (!defined('SITE_NAME')) define('SITE_NAME', 'Aura Farming');

if (!defined('ROOT_PATH'))       define('ROOT_PATH', dirname(__DIR__));
if (!defined('UPLOAD_PATH'))     define('UPLOAD_PATH', ROOT_PATH . '/assets/uploads/avatars/');
if (!defined('UPLOAD_URL'))      define('UPLOAD_URL', SITE_URL . '/assets/uploads/avatars/');
if (!defined('STORAGE_PATH'))    define('STORAGE_PATH', ROOT_PATH . '/storage/');
if (!defined('DEFAULT_AVATAR'))  define('DEFAULT_AVATAR', SITE_URL . '/assets/img/avatar_default.svg');

if (!defined('SESSION_LIFETIME'))   define('SESSION_LIFETIME', 86400 * 30);
if (!defined('AUTH_SESSION_DAYS'))  define('AUTH_SESSION_DAYS', 30);
if (!defined('CSRF_TOKEN_NAME'))    define('CSRF_TOKEN_NAME', '_csrf_token');
if (!defined('AUTH_COOKIE_NAME'))   define('AUTH_COOKIE_NAME', 'af_auth');
if (!defined('DEVICE_COOKIE_NAME')) define('DEVICE_COOKIE_NAME', 'af_device');

if (!defined('MAX_UPLOAD_SIZE')) define('MAX_UPLOAD_SIZE', 5 * 1024 * 1024);
if (!defined('ALLOWED_MIMES'))   define('ALLOWED_MIMES', ['image/jpeg', 'image/png', 'image/webp']);

if (!defined('MAIL_FROM'))      define('MAIL_FROM', (string)af_env('MAIL_FROM', 'no-reply@localhost'));
if (!defined('MAIL_FROM_NAME')) define('MAIL_FROM_NAME', 'Aura Farming');

if (!defined('AURA_MAX'))       define('AURA_MAX', 1000);
if (!defined('SESSAO_DURACAO')) define('SESSAO_DURACAO', 180);
if (!defined('LIKE_VALOR'))     define('LIKE_VALOR', 0.25);
if (!defined('SUPERLIKE_VALOR'))define('SUPERLIKE_VALOR', 1.0);
if (!defined('AURA_VISUAL_VERSION')) define('AURA_VISUAL_VERSION', '8.0');
if (!defined('COMPETITION_VERSION')) define('COMPETITION_VERSION', '10.0');
if (!defined('VIRAL_VERSION')) define('VIRAL_VERSION', '11.0');
if (!defined('SECURITY_VERSION')) define('SECURITY_VERSION', '12.0');
if (!defined('ADMIN_VERSION')) define('ADMIN_VERSION', '13.0');
if (!defined('AGE_PROTECTION_VERSION')) define('AGE_PROTECTION_VERSION', '14.0');
if (!defined('GUARDIAN_VERSION')) define('GUARDIAN_VERSION', '16.0');
if (!defined('GUARDIAN_VERIFICATION_VERSION')) define('GUARDIAN_VERIFICATION_VERSION', '16.4');
if (!defined('PARENTAL_CONSENT_VERSION')) define('PARENTAL_CONSENT_VERSION', '17.0');
if (!defined('MINOR_POLICY_VERSION')) define('MINOR_POLICY_VERSION', '18.4');
if (!defined('SAFE_CAMERA_VERSION')) define('SAFE_CAMERA_VERSION', '19.1');
if (!defined('GUARDIAN_DASHBOARD_VERSION')) define('GUARDIAN_DASHBOARD_VERSION', '20.1');
if (!defined('PRIVACY_CENTER_VERSION')) define('PRIVACY_CENTER_VERSION', '21.3');
if (!defined('MODERATION_VERSION')) define('MODERATION_VERSION', '22.0');
if (!defined('CONTROLLED_EVENT_VERSION')) define('CONTROLLED_EVENT_VERSION', '23.0');
if (!defined('STAGE24_AUDIT_VERSION')) define('STAGE24_AUDIT_VERSION', '24.0');
if (!defined('STAGE25_RELEASE_VERSION')) define('STAGE25_RELEASE_VERSION', '25.0');
if (!defined('TERMS_VERSION')) define('TERMS_VERSION', '1.2');
if (!defined('PRIVACY_POLICY_VERSION')) define('PRIVACY_POLICY_VERSION', '1.2');
if (!defined('CHILD_PRIVACY_VERSION')) define('CHILD_PRIVACY_VERSION', '1.2');
if (!defined('COOKIE_NOTICE_VERSION')) define('COOKIE_NOTICE_VERSION', '1.0');
if (!defined('GUARDIAN_TERMS_VERSION')) define('GUARDIAN_TERMS_VERSION', '1.2');
if (!defined('CAMERA_POLICY_VERSION')) define('CAMERA_POLICY_VERSION', '1.2');
if (!defined('RETENTION_POLICY_VERSION')) define('RETENTION_POLICY_VERSION', '1.2');
if (!defined('COMMUNITY_RULES_VERSION')) define('COMMUNITY_RULES_VERSION', '1.2');
if (!defined('GUARDIAN_VERIFICATION_DEBUG_CODES')) define('GUARDIAN_VERIFICATION_DEBUG_CODES', filter_var(af_env('GUARDIAN_VERIFICATION_DEBUG_CODES', false), FILTER_VALIDATE_BOOL));
if (!defined('SECURITY_HASH_SALT')) define('SECURITY_HASH_SALT', (string)af_env('SECURITY_HASH_SALT', 'troque-esta-chave-em-producao'));
if (!defined('ANTIFRAUD_REVIEW_SCORE')) define('ANTIFRAUD_REVIEW_SCORE', max(1, min(100, (int)af_env('ANTIFRAUD_REVIEW_SCORE', 40))));
if (!defined('ANTIFRAUD_REJECT_SCORE')) define('ANTIFRAUD_REJECT_SCORE', max(ANTIFRAUD_REVIEW_SCORE + 1, min(100, (int)af_env('ANTIFRAUD_REJECT_SCORE', 75))));
if (!defined('LIVENESS_ENABLED')) define('LIVENESS_ENABLED', filter_var(af_env('LIVENESS_ENABLED', true), FILTER_VALIDATE_BOOL));
if (!defined('LIVENESS_MIN_PASSES')) define('LIVENESS_MIN_PASSES', max(1, min(5, (int)af_env('LIVENESS_MIN_PASSES', 2))));
if (!defined('ADMIN_EMAILS')) define('ADMIN_EMAILS', array_values(array_filter(array_map('trim', explode(',', (string)af_env('ADMIN_EMAILS', ''))))));
if (!defined('LIVE_POLL_INTERVAL_MS')) define('LIVE_POLL_INTERVAL_MS', 1500);
if (!defined('LIVE_PRESENCE_TIMEOUT')) define('LIVE_PRESENCE_TIMEOUT', 12);
if (!defined('WEBRTC_ENABLED')) define('WEBRTC_ENABLED', filter_var(af_env('WEBRTC_ENABLED', true), FILTER_VALIDATE_BOOL));
if (!defined('WEBRTC_STUN_URL')) define('WEBRTC_STUN_URL', (string)af_env('WEBRTC_STUN_URL', 'stun:stun.l.google.com:19302'));
if (!defined('WEBRTC_TURN_URL')) define('WEBRTC_TURN_URL', (string)af_env('WEBRTC_TURN_URL', ''));
if (!defined('WEBRTC_TURN_USERNAME')) define('WEBRTC_TURN_USERNAME', (string)af_env('WEBRTC_TURN_USERNAME', ''));
if (!defined('WEBRTC_TURN_CREDENTIAL')) define('WEBRTC_TURN_CREDENTIAL', (string)af_env('WEBRTC_TURN_CREDENTIAL', ''));
if (!defined('WEBRTC_MAX_VIEWERS')) define('WEBRTC_MAX_VIEWERS', max(1, min(20, (int)af_env('WEBRTC_MAX_VIEWERS', 8))));
if (!defined('VISION_MODEL_VERSION')) define('VISION_MODEL_VERSION', 'mediapipe-tasks-vision-0.10.35');
if (!defined('VISION_POSE_MODEL_URL')) define('VISION_POSE_MODEL_URL', (string)af_env('VISION_POSE_MODEL_URL', 'https://storage.googleapis.com/mediapipe-models/pose_landmarker/pose_landmarker_lite/float16/latest/pose_landmarker_lite.task'));
if (!defined('VISION_HAND_MODEL_URL')) define('VISION_HAND_MODEL_URL', (string)af_env('VISION_HAND_MODEL_URL', 'https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/latest/hand_landmarker.task'));

if (!defined('TITULOS')) define('TITULOS', [
    1  => 'Aura Initiate',
    6  => 'Aura Farmer',
    16 => 'Aura Master',
    31 => 'Aura Legend',
    51 => 'Aura Overlord',
]);

$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
    || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');

if (session_status() === PHP_SESSION_NONE) {
    ini_set('session.cookie_httponly', '1');
    ini_set('session.use_strict_mode', '1');
    ini_set('session.use_only_cookies', '1');
    session_set_cookie_params([
        'lifetime' => SESSION_LIFETIME,
        'path'     => '/',
        'secure'   => $https,
        'httponly' => true,
        'samesite' => 'Lax',
    ]);
    session_start();
}

if (!isset($_SESSION['ultima_regeneracao'])) {
    $_SESSION['ultima_regeneracao'] = time();
} elseif (time() - (int)$_SESSION['ultima_regeneracao'] > 1800) {
    session_regenerate_id(true);
    $_SESSION['ultima_regeneracao'] = time();
}
