<?php
/**
 * نصب + تست بهای تمام‌شده فعالیت انتخابات
 * C:\xampp\php\php.exe run-election-abc-test.php
 */
declare(strict_types=1);

define('ACC_BASE_PATH', __DIR__);
require __DIR__ . '/standalone/bootstrap.php';
require_once ACC_PATH . 'includes/helpers/election-activity-cost.php';

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

Election_Activity_Cost::ensure_tables();

$campaign = Election_Activity_Cost::ensure_default_campaign();

if (!$campaign) {
    echo json_encode(['ok' => false, 'error' => 'کمپین یافت نشد'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    exit(1);
}

$alloc = Election_Activity_Cost::allocate($campaign);
Election_Activity_Cost::persist((int) $campaign['id'], $alloc);
$graph = Election_Activity_Cost::cost_graph(['campaign_id' => (int) $campaign['id']]);

echo json_encode([
    'ok' => true,
    'campaign_id' => (int) $campaign['id'],
    'campaign_key' => $campaign['campaign_key'] ?? null,
    'title' => $campaign['title'] ?? null,
    'pools' => $alloc['pools'],
    'person_shares' => $alloc['person_shares'],
    'lines_count' => count($alloc['lines']),
    'totals' => $alloc['totals'],
    'graph' => [
        'nodes' => count($graph['nodes']),
        'edges' => count($graph['edges']),
        'types' => array_count_values(array_column($graph['nodes'], 'type')),
    ],
    'ui' => 'http://localhost/acc-engine/election.php → ورود election_dept_financial / Election1234 → تب حسابداری فعالیت',
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
