<?php

namespace App\Http\Resources\ClassProgress;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ClassProgressReportResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $attachments = $this->attachments ?? $this['attachments'] ?? [];

        return [
            'id' => $this->id,
            'teacher_id' => $this->teacher_id,
            'teacher_name' => $this->teacher_name
                ?? trim((string) ($this->teacher?->firstname ?? '').' '.(string) ($this->teacher?->lastname ?? '')),
            'class_section_id' => $this->class_section_id,
            'class_section_name' => $this->class_section_name ?? $this->classSection?->class_section_name,
            'week_start' => $this->week_start?->format('Y-m-d'),
            'week_end' => $this->week_end?->format('Y-m-d'),
            'subject' => $this->subject,
            'unit_title' => $this->unit_title,
            'covered' => $this->covered,
            'homework' => $this->homework,
            'status' => $this->status,
            'status_label' => $this->status_label ?? (config('progress.status_options')[$this->status] ?? null),
            'support_needed' => $this->support_needed,
            'flags' => $this->flags_json,
            'attachment_path' => $this->attachment_path,
            'attachments' => ClassProgressAttachmentResource::collection(collect($attachments)),
            'created_at' => $this->created_at?->toIso8601String(),
            'updated_at' => $this->updated_at?->toIso8601String(),
        ];
    }
}
