<?php
/**
 * Education_Controller — پورتال EduBrain
 */
if ( ! defined( 'ABSPATH' ) ) exit;

require_once ACC_PATH . 'includes/helpers/education-platform.php';
require_once ACC_PATH . 'includes/helpers/education-ai.php';
require_once ACC_PATH . 'includes/helpers/education-gis.php';

class Education_Controller {

    public static function register_routes(): void {
        $ns  = 'acc/v1';
        $jwt = 'acc_jwt_check_permission';
        $pub = '__return_true';

        register_rest_route( $ns, '/education/meta', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'meta' ], 'permission_callback' => $pub,
        ] );
        register_rest_route( $ns, '/education/dashboard', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'dashboard' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/me', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'me' ], 'permission_callback' => $jwt,
        ] );

        register_rest_route( $ns, '/education/learner/profile', [
            [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_learner' ], 'permission_callback' => $jwt ],
            [ 'methods' => 'PUT', 'callback' => [ __CLASS__, 'update_learner' ], 'permission_callback' => $jwt ],
        ] );
        register_rest_route( $ns, '/education/companion/state', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'companion_state' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/companion/profile', [
            'methods' => 'PUT', 'callback' => [ __CLASS__, 'update_companion' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/lifepath', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'life_path' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/companion/report-import', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'import_report' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/companion/parent/approve', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'parent_approve_budget' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/parent/dashboard', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'parent_dashboard' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/matching', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'matching' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/graph/context', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'graph_context' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/learner/timeline', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'learner_timeline' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/learner/log', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'add_log' ], 'permission_callback' => $jwt,
        ] );

        register_rest_route( $ns, '/education/requests', [
            [ 'methods' => 'GET',  'callback' => [ __CLASS__, 'list_requests' ], 'permission_callback' => $jwt ],
            [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'create_request' ], 'permission_callback' => $jwt ],
        ] );
        register_rest_route( $ns, '/education/requests/(?P<id>\d+)', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'get_request' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/requests/(?P<id>\d+)/test', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'get_request_test' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/requests/(?P<id>\d+)/submit', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'submit_request_test' ], 'permission_callback' => $jwt,
        ] );

        register_rest_route( $ns, '/education/educator/profile', [
            [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_educator' ], 'permission_callback' => $jwt ],
            [ 'methods' => 'PUT', 'callback' => [ __CLASS__, 'update_educator' ], 'permission_callback' => $jwt ],
        ] );
        register_rest_route( $ns, '/education/educators', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'list_educators' ], 'permission_callback' => $jwt,
        ] );

        register_rest_route( $ns, '/education/orgs', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'list_orgs' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/orgs/(?P<id>\d+)/resources', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'org_resources' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/orgs/(?P<id>\d+)/schedule', [
            [ 'methods' => 'GET',  'callback' => [ __CLASS__, 'org_schedule' ], 'permission_callback' => $jwt ],
            [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'save_schedule' ], 'permission_callback' => $jwt ],
        ] );

        register_rest_route( $ns, '/education/sessions/quote', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'quote_session' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/sessions', [
            [ 'methods' => 'GET',  'callback' => [ __CLASS__, 'list_sessions' ], 'permission_callback' => $jwt ],
            [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'create_session' ], 'permission_callback' => $jwt ],
        ] );
        register_rest_route( $ns, '/education/sessions/(?P<id>\d+)/assign', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'assign_session' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/sessions/(?P<id>\d+)/complete', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'complete_session' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/dispatch/queue', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'dispatch_queue' ], 'permission_callback' => $jwt,
        ] );

        register_rest_route( $ns, '/education/placement', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'placement' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/plan', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'active_plan' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/brain/state', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'brain_state' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/assessments', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'list_assessments' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/sessions/(?P<id>\d+)', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'get_session' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/orgs/(?P<id>\d+)', [
            [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_org' ], 'permission_callback' => $jwt ],
            [ 'methods' => 'PUT', 'callback' => [ __CLASS__, 'update_org' ], 'permission_callback' => $jwt ],
        ] );

        register_rest_route( $ns, '/education/ai/chat', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'ai_chat' ], 'permission_callback' => $jwt,
        ] );

        register_rest_route( $ns, '/education/visitor/quote', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'visitor_quote' ], 'permission_callback' => $pub,
        ] );
        register_rest_route( $ns, '/education/visitor/ai', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'visitor_ai' ], 'permission_callback' => $pub,
        ] );

        register_rest_route( $ns, '/education/gis/map', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'gis_map' ], 'permission_callback' => $jwt,
        ] );
        register_rest_route( $ns, '/education/demo-users', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'demo_users' ], 'permission_callback' => $pub,
        ] );
    }

    private static function uid(): int {
        return (int) get_current_user_id();
    }

    public static function user_role( int $uid ): string {
        if ( $uid <= 0 ) {
            return 'guest';
        }
        if ( function_exists( 'acl_user_is_super_admin' ) && acl_user_is_super_admin( $uid ) ) {
            return 'accountant';
        }
        foreach ( [
            'accountant' => [ 'education.accounting', 'education.accounting.post' ],
            'org_admin'  => [ 'education.org' ],
            'mentor'     => [ 'education.mentor' ],
        ] as $role => $perms ) {
            foreach ( $perms as $pk ) {
                if ( function_exists( 'acl_user_can' ) && acl_user_can( $uid, $pk ) ) {
                    return $role;
                }
            }
        }
        global $wpdb;
        $teacher = (int) $wpdb->get_var( $wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->prefix}acl_user_roles ur
             INNER JOIN {$wpdb->prefix}acl_roles r ON r.id = ur.role_id
             WHERE ur.user_id = %d AND r.slug = 'edu_teacher'", $uid
        ) );
        if ( $teacher ) {
            return 'teacher';
        }
        $parent = (int) $wpdb->get_var( $wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->prefix}acl_user_roles ur
             INNER JOIN {$wpdb->prefix}acl_roles r ON r.id = ur.role_id
             WHERE ur.user_id = %d AND r.slug = 'edu_parent'", $uid
        ) );
        if ( $parent ) {
            return 'parent';
        }
        return 'learner';
    }

    private static function ok( $data, int $code = 200 ): WP_REST_Response {
        return new WP_REST_Response( is_array( $data ) ? $data : [ 'data' => $data ], $code );
    }

    public static function meta(): WP_REST_Response {
        return self::ok( Education_Platform::public_meta() );
    }

    public static function me( WP_REST_Request $req ): WP_REST_Response {
        $uid = self::uid();
        return self::ok( [
            'user_id' => $uid,
            'role'    => self::user_role( $uid ),
            'display' => wp_get_current_user()->display_name ?? '',
        ] );
    }

    public static function dashboard( WP_REST_Request $req ): WP_REST_Response {
        $uid  = self::uid();
        $role = sanitize_key( (string) ( $req->get_param( 'role' ) ?: self::user_role( $uid ) ) );
        if ( ! in_array( $role, [ 'learner', 'parent', 'mentor', 'teacher', 'org_admin', 'accountant' ], true ) ) {
            $role = self::user_role( $uid );
        }
        return self::ok( Education_Platform::dashboard( $uid, $role ) );
    }

    public static function get_learner(): WP_REST_Response {
        return self::ok( Education_Platform::get_or_create_learner( self::uid() ) );
    }

    public static function update_learner( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        return self::ok( Education_Platform::update_learner( self::uid(), $body ) );
    }

    public static function companion_state(): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-lifepath.php';
        return self::ok( Edu_LifePath::companion_state( self::uid() ) );
    }

    public static function update_companion( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $learner = Education_Platform::update_learner( self::uid(), $body );
        require_once ACC_PATH . 'includes/helpers/education-lifepath.php';
        return self::ok( [
            'success'   => true,
            'learner'   => $learner,
            'life_path' => Edu_LifePath::generate_path( $learner, Education_Platform::active_plan( self::uid() ) ),
        ] );
    }

    public static function life_path(): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-lifepath.php';
        $learner = Education_Platform::get_or_create_learner( self::uid() );
        return self::ok( Edu_LifePath::generate_path( $learner, Education_Platform::active_plan( self::uid() ) ) );
    }

    public static function import_report( WP_REST_Request $req ): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
        $meta = [
            'grade'      => $req->get_param( 'grade' ),
            'year'       => $req->get_param( 'year' ),
            'report_avg' => $req->get_param( 'report_avg' ),
            'school_name'=> $req->get_param( 'school_name' ),
            'teacher_name'=> $req->get_param( 'teacher_name' ),
            'notes'      => $req->get_param( 'notes' ),
        ];
        if ( ! empty( $_FILES['report'] ) ) {
            return self::ok( Edu_Companion_Extras::import_report_card( self::uid(), $_FILES['report'], $meta ) );
        }
        $body = $req->get_json_params() ?: [];
        if ( ! empty( $body['content_base64'] ) ) {
            $bin  = base64_decode( (string) $body['content_base64'], true );
            $tmp  = wp_tempnam( 'edu_rc' );
            file_put_contents( $tmp, $bin );
            $file = [
                'tmp_name' => $tmp,
                'name'     => sanitize_file_name( $body['filename'] ?? 'report.jpg' ),
                'type'     => sanitize_text_field( $body['mime'] ?? 'image/jpeg' ),
                'size'     => strlen( $bin ),
            ];
            $meta = array_merge( $meta, $body );
            $result = Edu_Companion_Extras::import_report_card( self::uid(), $file, $meta, true );
            @unlink( $tmp );
            return self::ok( $result );
        }
        return self::ok( [ 'success' => false, 'message' => 'فایل ارسال نشد' ], 400 );
    }

    public static function parent_approve_budget( WP_REST_Request $req ): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
        $body = $req->get_json_params() ?: [];
        $learner_uid = (int) ( $body['learner_user_id'] ?? 0 );
        $role = self::user_role( self::uid() );
        if ( $role === 'learner' ) {
            $learner_uid = self::uid();
        } elseif ( $role === 'parent' && $learner_uid <= 0 ) {
            require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
            $link = Edu_Companion_Extras::parent_link( self::uid() );
            $learner_uid = (int) ( $link['learner_user_id'] ?? 0 );
        }
        if ( $learner_uid <= 0 ) {
            return self::ok( [ 'success' => false, 'message' => 'شناسه دانش‌آموز نامعتبر' ], 400 );
        }
        return self::ok( Edu_Companion_Extras::approve_budget( self::uid(), $learner_uid, $body ) );
    }

    public static function parent_dashboard(): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
        return self::ok( Edu_Companion_Extras::parent_dashboard( self::uid() ) );
    }

    public static function matching( WP_REST_Request $req ): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
        $domain = $req->get_param( 'domain' );
        return self::ok( Edu_Companion_Extras::match_recommendations( self::uid(), $domain ? sanitize_key( $domain ) : null ) );
    }

    public static function graph_context(): WP_REST_Response {
        require_once ACC_PATH . 'includes/helpers/education-companion-extras.php';
        return self::ok( Edu_Companion_Extras::graph_rag_context( self::uid() ) );
    }

    public static function learner_timeline(): WP_REST_Response {
        return self::ok( Education_Platform::learner_timeline( self::uid() ) );
    }

    public static function add_log( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $type = sanitize_key( $body['log_type'] ?? 'academic' );
        $cat  = sanitize_key( $body['category'] ?? 'general' );
        $id   = Education_Platform::add_learner_log( self::uid(), $type, $cat, $body );
        return self::ok( [ 'success' => true, 'id' => $id ] );
    }

    public static function list_requests(): WP_REST_Response {
        return self::ok( [ 'requests' => Education_Platform::list_service_requests( self::uid() ) ] );
    }

    public static function create_request( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::create_service_request( self::uid(), $req->get_json_params() ?: [] ) );
    }

    public static function get_request( WP_REST_Request $req ): WP_REST_Response {
        $r = Education_Platform::get_service_request( (int) $req['id'], self::uid() );
        if ( ! $r ) {
            return self::ok( [ 'success' => false, 'message' => 'یافت نشد' ], 404 );
        }
        return self::ok( [ 'request' => $r ] );
    }

    public static function get_request_test( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::get_assessment_test( (int) $req['id'], self::uid() ) );
    }

    public static function submit_request_test( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        return self::ok( Education_Platform::submit_assessment(
            (int) $req['id'],
            self::uid(),
            is_array( $body['answers'] ?? null ) ? $body['answers'] : []
        ) );
    }

    public static function get_educator(): WP_REST_Response {
        return self::ok( Education_Platform::get_or_create_educator( self::uid() ) );
    }

    public static function update_educator( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::update_educator( self::uid(), $req->get_json_params() ?: [] ) );
    }

    public static function list_educators( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::list_educators( [
            'verified' => (bool) $req->get_param( 'verified' ),
        ] ) );
    }

    public static function list_orgs( WP_REST_Request $req ): WP_REST_Response {
        $type = $req->get_param( 'type' );
        return self::ok( Education_Platform::list_orgs( $type ? sanitize_key( $type ) : null ) );
    }

    public static function org_resources( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::org_resources( (int) $req['id'] ) );
    }

    public static function org_schedule( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::org_schedule(
            (int) $req['id'],
            $req->get_param( 'from' ),
            $req->get_param( 'to' )
        ) );
    }

    public static function save_schedule( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::save_org_schedule_slot(
            (int) $req['id'],
            $req->get_json_params() ?: []
        ) );
    }

    public static function quote_session( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Edu_Cost_Accounting::quote_session( $req->get_json_params() ?: [] ) );
    }

    public static function create_session( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::create_session_request( self::uid(), $req->get_json_params() ?: [] ) );
    }

    public static function list_sessions( WP_REST_Request $req ): WP_REST_Response {
        global $wpdb;
        $uid  = self::uid();
        $role = self::user_role( $uid );
        $st   = Education_Platform::tbl( 'sessions' );
        if ( in_array( $role, [ 'mentor', 'teacher' ], true ) ) {
            $ed = Education_Platform::get_or_create_educator( $uid );
            $rows = $wpdb->get_results( $wpdb->prepare(
                "SELECT * FROM {$st} WHERE educator_id = %d ORDER BY id DESC LIMIT 50", (int) $ed['id']
            ), ARRAY_A ) ?: [];
        } else {
            $learner = Education_Platform::get_or_create_learner( $uid );
            $rows = $wpdb->get_results( $wpdb->prepare(
                "SELECT * FROM {$st} WHERE learner_id = %d ORDER BY id DESC LIMIT 50", (int) $learner['id']
            ), ARRAY_A ) ?: [];
        }
        return self::ok( [ 'sessions' => $rows ] );
    }

    public static function assign_session( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Education_Platform::assign_session( (int) $req['id'], self::uid() ) );
    }

    public static function complete_session( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        return self::ok( Education_Platform::complete_session( (int) $req['id'], self::uid(), $body ) );
    }

    public static function dispatch_queue( WP_REST_Request $req ): WP_REST_Response {
        $role = self::user_role( self::uid() );
        $uid  = in_array( $role, [ 'mentor', 'teacher' ], true ) ? self::uid() : null;
        return self::ok( [
            'mine'    => $uid ? Education_Platform::dispatch_queue( $uid ) : [],
            'pending' => Education_Platform::dispatch_queue(),
        ] );
    }

    public static function placement( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $answers = is_array( $body['answers'] ?? null ) ? $body['answers'] : [];
        return self::ok( Education_Platform::run_placement( self::uid(), $answers ) );
    }

    public static function active_plan(): WP_REST_Response {
        return self::ok( [ 'plan' => Education_Platform::active_plan( self::uid() ) ] );
    }

    public static function brain_state(): WP_REST_Response {
        return self::ok( Education_Platform::brain_state( self::uid() ) );
    }

    public static function list_assessments(): WP_REST_Response {
        return self::ok( [ 'assessments' => Education_Platform::list_assessments( self::uid() ) ] );
    }

    public static function get_session( WP_REST_Request $req ): WP_REST_Response {
        $s = Education_Platform::get_session( (int) $req['id'] );
        if ( ! $s ) {
            return self::ok( [ 'success' => false, 'message' => 'جلسه یافت نشد' ], 404 );
        }
        return self::ok( [ 'session' => $s ] );
    }

    public static function get_org( WP_REST_Request $req ): WP_REST_Response {
        $o = Education_Platform::get_org( (int) $req['id'] );
        if ( ! $o ) {
            return self::ok( [ 'success' => false, 'message' => 'مؤسسه یافت نشد' ], 404 );
        }
        $o['resources'] = Education_Platform::org_resources( (int) $req['id'] );
        $o['schedule']  = Education_Platform::org_schedule( (int) $req['id'] );
        return self::ok( [ 'org' => $o ] );
    }

    public static function update_org( WP_REST_Request $req ): WP_REST_Response {
        $o = Education_Platform::update_org( (int) $req['id'], $req->get_json_params() ?: [] );
        if ( ! $o ) {
            return self::ok( [ 'success' => false, 'message' => 'مؤسسه یافت نشد' ], 404 );
        }
        return self::ok( [ 'success' => true, 'org' => $o ] );
    }

    public static function ai_chat( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $msg  = sanitize_text_field( $body['message'] ?? '' );
        if ( $msg === '' ) {
            return self::ok( [ 'success' => false, 'message' => 'پیام خالی است' ], 400 );
        }
        return self::ok( Edu_AI::chat( self::uid(), $msg, $body ) );
    }

    public static function visitor_quote( WP_REST_Request $req ): WP_REST_Response {
        return self::ok( Edu_Cost_Accounting::quote_session( $req->get_json_params() ?: [] ) );
    }

    public static function visitor_ai( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $msg  = sanitize_text_field( $body['message'] ?? '' );
        return self::ok( Edu_AI::chat( 0, $msg, $body ) );
    }

    public static function gis_map(): WP_REST_Response {
        return self::ok( Edu_GIS::map_payload() );
    }

    public static function demo_users(): WP_REST_Response {
        if ( ! function_exists( 'acc_demo_endpoints_allowed' ) || ! acc_demo_endpoints_allowed() ) {
            return new WP_REST_Response( [ 'success' => false, 'message' => 'غیرفعال' ], 404 );
        }
        require_once ACC_PATH . 'includes/helpers/education-seed.php';
        $pass = (string) get_option( 'acc_education_demo_password', Education_Seed::DEMO_PASS );
        $rows = [];
        foreach ( Education_Seed::demo_users() as [ $login, $name, $role ] ) {
            $rows[] = [
                'username' => $login,
                'password' => $pass,
                'label'    => $name,
                'role'     => str_replace( 'edu_', '', $role ),
            ];
        }
        return self::ok( [ 'password' => $pass, 'users' => $rows ] );
    }
}
