<?php
require_once __DIR__ . '/config.php';

function getDB() {
    static $pdo = null;
    if ($pdo === null) {
        $pdo = new PDO(
            "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8",
            DB_USER, DB_PASS,
            [
                PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
            ]
        );
    }
    return $pdo;
}

function generateKey() {
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $seg = function() use ($chars) {
        $s = '';
        for ($i = 0; $i < 4; $i++) $s .= $chars[random_int(0, strlen($chars)-1)];
        return $s;
    };
    return $seg() . '-' . $seg() . '-' . $seg() . '-' . $seg();
}

function logVerify($key, $deviceId, $ip, $result, $message) {
    try {
        $db = getDB();
        $stmt = $db->prepare("INSERT INTO verify_logs (lic_key, device_id, ip, result, message) VALUES (?,?,?,?,?)");
        $stmt->execute([$key, $deviceId, $ip, $result, $message]);
    } catch (Exception $e) { /* sessizce geç */ }
}
