<?php
/**
 * تست end-to-end ماژول جذب کارشناس
 * C:\xampp\php\php.exe run-service-talent-test.php
 */
declare(strict_types=1);

$base = 'http://localhost/acc-engine/api.php/acc/v1';

function api(string $method, string $path, ?array $body = null): array {
    global $base;
    $url = $base . $path;
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_HTTPHEADER     => ['Content-Type: application/json', 'Accept: application/json'],
    ]);
    if ($body !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_UNICODE));
    }
    $raw = curl_exec($ch);
    $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    $json = json_decode($raw ?: '{}', true) ?: [];
    return ['code' => $code, 'json' => $json, 'raw' => $raw];
}

$steps = [];

$r = api('GET', '/service/talent/meta');
$steps['meta'] = $r['code'] === 200 && ($r['json']['success'] ?? false);

$r = api('POST', '/service/talent/apply', [
    'full_name' => 'تست جذب دمو',
    'phone'     => '09129990001',
    'email'     => 'talent.test@demo.local',
    'city'      => 'تهران',
    'source_channel' => 'linkedin',
]);
$token = $r['json']['data']['access_token'] ?? '';
$steps['apply'] = $r['code'] === 201 && strlen($token) === 48;

$r = api('PUT', "/service/talent/apply/{$token}/identity", [
    'video_intro_url' => 'https://www.aparat.com/v/demo',
    'identities' => [
        ['platform' => 'linkedin', 'profile_url' => 'https://linkedin.com/in/demo', 'username' => 'demo'],
        ['platform' => 'portfolio', 'profile_url' => 'https://example.com/portfolio', 'username' => ''],
    ],
    'certificates' => [
        ['title' => 'HVAC Level 1', 'issuer' => 'Demo Institute', 'cert_url' => 'https://example.com/cert', 'year' => 2024],
    ],
]);
$steps['identity'] = $r['code'] === 200 && ($r['json']['data']['digital_presence_score'] ?? 0) > 0;

$r = api('PUT', "/service/talent/apply/{$token}/profile", [
    'years_experience' => 5,
    'primary_specialty' => 'chiller',
    'secondary_skills' => ['VRF', 'BMS'],
    'bio' => str_repeat('تجربه کار با چیلر و سردخانه در پروژه‌های صنعتی. ', 3),
    'cover_letter' => 'علاقه‌مند به پیوستن به شبکه خدمات میدانی.',
    'preferred_work_type' => 'contract',
]);
$steps['profile'] = $r['code'] === 200;

$r = api('POST', "/service/talent/apply/{$token}/assessment/start");
$questions = $r['json']['data']['questions'] ?? [];
$steps['assessment_start'] = $r['code'] === 200 && count($questions) === 15;

// پاسخ اولین گزینه هر سؤال (برای تست مسیر — نه دقت نمره)
$answers = [];
foreach ($questions as $q) {
    if (!empty($q['options'][0]['id'])) {
        $answers[$q['id']] = $q['options'][0]['id'];
    }
}

$r = api('POST', "/service/talent/apply/{$token}/assessment/submit", ['answers' => $answers]);
$steps['assessment_submit'] = $r['code'] === 200 && isset($r['json']['data']['application']);

$r = api('GET', "/service/talent/apply/{$token}/result");
$steps['result'] = $r['code'] === 200 && ($r['json']['data']['application']['applicant_no'] ?? '') !== '';

$ok = !in_array(false, $steps, true);

echo json_encode([
    'success' => $ok,
    'token'   => $token,
    'applicant_no' => $r['json']['data']['application']['applicant_no'] ?? null,
    'steps'   => $steps,
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
