<?php
declare(strict_types=1);

define('ACC_BASE_PATH', __DIR__);
require __DIR__ . '/standalone/bootstrap.php';

header('Content-Type: application/json; charset=utf-8');

global $wpdb;
if (method_exists($wpdb, 'query')) {
    $wpdb->query('SET NAMES utf8mb4');
}

$name  = "\u{0627}\u{0628}\u{0631}\u{0627}\u{0647}\u{06CC}\u{0645} \u{0639}\u{0632}\u{06CC}\u{0632}\u{06CC}"; // ابراهیم عزیزی
$title = "\u{0631}\u{0626}\u{06CC}\u{0633} \u{06A9}\u{0645}\u{06CC}\u{0633}\u{06CC}\u{0648}\u{0646} \u{0627}\u{0645}\u{0646}\u{06CC}\u{062A} \u{0645}\u{0644}\u{06CC}"; // رئیس کمیسیون امنیت ملی
$title .= ' — Shiraz MP';
$photo = '/acc-engine/assets/img/election/azizi.jpg';

$debug = [];
foreach ([
    'acc_election_candidate_name'  => $name,
    'acc_election_candidate_title' => $title,
    'acc_election_candidate_photo' => $photo,
] as $key => $val) {
    $sql = $wpdb->prepare(
        "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s",
        $val,
        $key
    );
    $r = $wpdb->query($sql);
    $debug[$key] = [
        'write' => $r,
        'error' => $wpdb->last_error ?? '',
        'read'  => $wpdb->get_var($wpdb->prepare(
            "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1",
            $key
        )),
    ];
}

echo json_encode(['ok' => true, 'debug' => $debug], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
