<?php
/**
 * همگام‌سازی کد نوسازی پرونده‌های ملک از GIS
 * php run-melk-sync-renovation-codes.php
 */
declare(strict_types=1);

define('ACC_BASE_PATH', __DIR__);
require __DIR__ . '/standalone/bootstrap.php';
require_once ACC_PATH . 'includes/controllers/Eqtesad_Melk_Controller.php';

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

$GLOBALS['_melk_install_quiet'] = true;
ob_start();
include __DIR__ . '/run-melk-install.php';
ob_end_clean();

$updated = Eqtesad_Melk_Controller::sync_case_renovation_codes();

global $wpdb;
$pre = $wpdb->prefix;
$cases = $wpdb->get_results(
    "SELECT id, case_key, renovation_code, parcel_id, block_code FROM {$pre}melk_cases ORDER BY id",
    ARRAY_A
);

echo json_encode([
    'success' => true,
    'updated' => $updated,
    'cases'   => $cases,
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
