<?php

namespace App\Resources;

use Wibiesana\Padi\Core\Resource;

class ExamResultResource extends Resource
{
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'exam_id' => $this->exam_id,
            'student_id' => $this->student_id,
            'exam_status_id' => $this->exam_status_id,
            'contain_essay' => $this->contain_essay,
            'attemp' => $this->attemp,
            'essay_result' => $this->essay_result,
            'mc_result' => $this->mc_result,
            'total_result' => $this->total_result,
            'class_id' => $this->class_id,
            'exam_giver_id' => $this->exam_giver_id,
            'answer_score_list' => $this->answer_score_list,
            'status' => $this->status,

            // Relations
            'class' => $this->whenLoaded('class'),
            'createdBy' => $this->whenLoaded('createdBy'),
            'examGiver' => $this->whenLoaded('examGiver'),
            'exam' => $this->whenLoaded('exam'),
            'student' => $this->whenLoaded('student'),
            'updatedBy' => $this->whenLoaded('updatedBy'),

            // Flattened Fields
            'class_name' => is_array($this->class) ? ($this->class['name'] ?? null) : ($this->class->name ?? null),
            'createdBy_name' => is_array($this->createdBy) ? ($this->createdBy['username'] ?? null) : ($this->createdBy->username ?? null),
            'examGiver_name' => is_array($this->examGiver) ? ($this->examGiver['name'] ?? null) : ($this->examGiver->name ?? null),
            'exam_name' => is_array($this->exam) ? ($this->exam['name'] ?? null) : ($this->exam->name ?? null),
            'student_name' => is_array($this->student) ? ($this->student['name'] ?? null) : ($this->student->name ?? null),
            'student_nis' => is_array($this->student) ? ($this->student['nis'] ?? null) : ($this->student->nis ?? null),
            'student_nisn' => is_array($this->student) ? ($this->student['nisn'] ?? null) : ($this->student->nisn ?? null),
            'updatedBy_name' => is_array($this->updatedBy) ? ($this->updatedBy['username'] ?? null) : ($this->updatedBy->username ?? null),

        ];
    }
}
