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

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

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

global $wpdb;
Edu_Activity_Cost::ensure_tables();

$session = $wpdb->get_row(
    "SELECT * FROM {$wpdb->prefix}edu_sessions ORDER BY id DESC LIMIT 1",
    ARRAY_A
);

if ( ! $session ) {
    $learner_id = (int) $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}edu_learners ORDER BY id ASC LIMIT 1" );
    if ( ! $learner_id ) {
        echo wp_json_encode( [
            'ok' => false,
            'error' => 'هیچ جلسه‌ای نیست — ابتدا run-education-seed.php را اجرا کنید',
        ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
        exit( 1 );
    }
    $quote = [
        'total' => 1350000,
        'lines' => [
            [ 'code' => 'mentor_fee', 'amount' => 800000 ],
            [ 'code' => 'venue_fee', 'amount' => 150000 ],
            [ 'code' => 'transport', 'amount' => 80000 ],
            [ 'code' => 'materials', 'amount' => 70000 ],
            [ 'code' => 'overhead', 'amount' => 60000 ],
            [ 'code' => 'platform', 'amount' => 90000 ],
        ],
    ];
    $wpdb->insert( "{$wpdb->prefix}edu_sessions", [
        'learner_id'    => $learner_id,
        'session_type'  => 'academic',
        'track'         => 'konkur',
        'domain'        => 'physics',
        'delivery_mode' => 'in_person',
        'venue_type'    => 'institute',
        'duration_min'  => 90,
        'status'        => 'completed',
        'quote_json'    => wp_json_encode( $quote, JSON_UNESCAPED_UNICODE ),
        'amount_total'  => 1350000,
        'notes'         => 'جلسه آزمایشی ABC',
        'created_at'    => current_time( 'mysql' ),
    ] );
    $sid = (int) $wpdb->insert_id;
    $session = Edu_Activity_Cost::get_session( $sid );
}

if ( ! $session ) {
    echo wp_json_encode( [ 'ok' => false, 'error' => 'جلسه آموزشی یافت نشد' ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
    exit( 1 );
}

$alloc = Edu_Activity_Cost::allocate( $session );
Edu_Activity_Cost::persist( (int) $session['id'], $alloc );
$graph = Edu_Activity_Cost::cost_graph( [ 'session_id' => (int) $session['id'] ] );

echo wp_json_encode( [
    'ok' => true,
    'session_id' => (int) $session['id'],
    'title' => trim( ( $session['session_type'] ?? '' ) . ' · ' . ( $session['domain'] ?? '' ) ),
    '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/education.php → ورود حسابدار → تب گراف ABC',
    'demo' => [ 'user' => 'edu_accountant', 'pass' => 'Edu@1404' ],
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
