<?php

namespace App\Http\Resources\Messaging;

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

class MessageResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id ?? null,
            'sender_id' => $this->sender_id ?? null,
            'recipient_id' => $this->recipient_id ?? null,
            'subject' => $this->subject ?? null,
            'message' => $this->message ?? null,
            'sent_datetime' => optional($this->sent_datetime)->toDateTimeString(),
            'read_status' => (bool) ($this->read_status ?? false),
            'read_datetime' => optional($this->read_datetime)->toDateTimeString(),
            'message_number' => $this->message_number ?? null,
            'priority' => $this->priority ?? null,
            'attachment' => $this->attachment ?? null,
            'status' => $this->status ?? null,
            'semester' => $this->semester ?? null,
            'school_year' => $this->school_year ?? null,
            'sender_name' => $this->sender ? trim($this->sender->firstname . ' ' . $this->sender->lastname) : null,
            'recipient_name' => $this->recipient ? trim($this->recipient->firstname . ' ' . $this->recipient->lastname) : null,
        ];
    }
}
