<?php
/**
 * Smoke test — Phase 2: report import · parent · matching · graph
 */
require __DIR__ . '/standalone/bootstrap.php';

require_once ACC_PATH . 'includes/helpers/education-platform.php';
require_once ACC_PATH . 'includes/helpers/education-lifepath.php';
require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
require_once ACC_PATH . 'includes/helpers/education-seed.php';

global $wpdb;

echo "=== EduBrain Phase 2 Smoke Test ===\n";

$seed = Education_Seed::run( false );
echo "Seed version: " . ( $seed['version'] ?? '?' ) . "\n";

$student_uid = (int) $wpdb->get_var( $wpdb->prepare(
    "SELECT ID FROM {$wpdb->users} WHERE user_login = %s", 'edu_student_konkur'
) );
$parent_uid = (int) $wpdb->get_var( $wpdb->prepare(
    "SELECT ID FROM {$wpdb->users} WHERE user_login = %s", 'edu_parent_konkur'
) );

if ( ! $student_uid ) {
    echo "FAIL: edu_student_konkur not found\n";
    exit( 1 );
}
echo "Student UID: $student_uid\n";
echo "Parent UID: " . ( $parent_uid ?: 'MISSING' ) . "\n";

$state = Edu_LifePath::companion_state( $student_uid );
$chart = $state['chart'] ?? [];
echo "Chart labels: " . count( $chart['labels'] ?? [] ) . " trend=" . ( $chart['trend'] ?? '?' ) . "\n";

$match = Edu_Companion_Extras::match_recommendations( $student_uid );
echo "Matching orgs: " . count( $match['orgs'] ?? [] ) . " educators: " . count( $match['educators'] ?? [] ) . "\n";

$graph = Edu_Companion_Extras::graph_rag_context( $student_uid );
echo "Graph-RAG nodes: " . ( $graph['node_count'] ?? 0 ) . " edges: " . ( $graph['edge_count'] ?? 0 ) . "\n";

$tmp = wp_tempnam( 'test_rc' );
file_put_contents( $tmp, base64_decode( '/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCwAA8A/9k=' ) );
$file = [ 'tmp_name' => $tmp, 'name' => 'report_18.5.jpg', 'type' => 'image/jpeg', 'size' => filesize( $tmp ) ];
$import = Edu_Companion_Extras::import_report_card( $student_uid, $file, [
    'grade' => '11', 'year' => '1404', 'report_avg' => 18.5, 'school_name' => 'دبیرستان تست',
], true );
@unlink( $tmp );
echo "Report import: " . ( $import['success'] ? 'OK' : 'FAIL ' . ( $import['message'] ?? '' ) ) . "\n";

if ( $parent_uid ) {
    $link = Edu_Companion_Extras::parent_link( $parent_uid );
    echo "Parent link learner: " . ( $link['learner_user_id'] ?? 'none' ) . "\n";
    $dash = Edu_Companion_Extras::parent_dashboard( $parent_uid );
    echo "Parent dashboard: " . ( $dash['success'] ? 'OK' : 'FAIL ' . ( $dash['message'] ?? '' ) ) . "\n";
    $approve = Edu_Companion_Extras::approve_budget( $parent_uid, $student_uid, [
        'monthly_budget_rial' => 6500000,
        'parent_notes'        => 'تأیید تست فاز ۲',
    ] );
    echo "Budget approve: " . ( $approve['success'] ? 'OK' : 'FAIL' ) . "\n";
}

echo "=== DONE ===\n";
