<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\MemberTransfer;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin MemberTransfer
 */
class TransferResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'member' => $this->whenLoaded('member', fn () => [
                'id' => $this->member->id,
                'member_number' => $this->member->member_number,
                'full_name' => trim("{$this->member->first_name} {$this->member->last_name}"),
            ]),
            'from_branch' => $this->whenLoaded('fromBranch', fn () => [
                'id' => $this->fromBranch->id,
                'name' => $this->fromBranch->name,
                'code' => $this->fromBranch->code,
            ]),
            'to_branch' => $this->whenLoaded('toBranch', fn () => [
                'id' => $this->toBranch->id,
                'name' => $this->toBranch->name,
                'code' => $this->toBranch->code,
            ]),
            'status' => $this->status,
            'reason' => $this->reason,
            'requested_by' => $this->whenLoaded('requestedBy', fn () => $this->requestedBy ? [
                'id' => $this->requestedBy->id,
                'name' => $this->requestedBy->name,
            ] : null),
            'decided_by' => $this->whenLoaded('decidedBy', fn () => $this->decidedBy ? [
                'id' => $this->decidedBy->id,
                'name' => $this->decidedBy->name,
            ] : null),
            'decided_at' => $this->decided_at?->toIso8601String(),
            'created_at' => $this->created_at?->toIso8601String(),
        ];
    }
}
