<?php

namespace App\Http\Resources\Promotions;

use Illuminate\Http\Resources\Json\JsonResource;

/**
 * Admin/list shape for `student_promotion_records`. Accepts either an
 * Eloquent model or an associative array (so list endpoints can
 * decorate the row with student/parent context once and pass it
 * through unchanged).
 */
class StudentPromotionResource extends JsonResource
{
    public function toArray($request): array
    {
        $r = $this->resource;
        $get = static function (string $key, $default = null) use ($r) {
            if (is_array($r)) {
                return $r[$key] ?? $default;
            }
            return $r->{$key} ?? $default;
        };

        return [
            'promotion_id' => (int) ($get('promotion_id') ?? 0),
            'student_id' => (int) ($get('student_id') ?? 0),
            'student_name' => $get('student_name'),
            'school_id' => $get('school_id'),
            'parent_id' => $get('parent_id') ? (int) $get('parent_id') : null,
            'parent_name' => $get('parent_name'),
            'parent_email' => $get('parent_email'),
            'current_school_year' => $get('current_school_year'),
            'next_school_year' => $get('next_school_year'),
            'current_level' => $get('current_level') ?? $get('current_level_name'),
            'promoted_level' => $get('promoted_level') ?? $get('promoted_level_name'),
            'promotion_status' => $get('promotion_status'),
            'enrollment_status' => $get('enrollment_status'),
            'enrollment_deadline' => $get('enrollment_deadline'),
            'parent_notified_at' => $get('parent_notified_at'),
            'enrollment_started_at' => $get('enrollment_started_at'),
            'enrollment_completed_at' => $get('enrollment_completed_at'),
            'promotion_finalized_at' => $get('promotion_finalized_at'),
            'final_average' => $get('final_average') !== null ? (float) $get('final_average') : null,
            'passed_current_level' => $get('passed_current_level'),
            'checklist' => $get('checklist'),
            'eligibility_notes' => $get('eligibility_notes'),
            'enrollment_id' => $get('enrollment_id') ? (int) $get('enrollment_id') : null,
            'updated_by' => $get('updated_by') ? (int) $get('updated_by') : null,
            'updated_at' => $get('updated_at'),
        ];
    }
}
