<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

/**
 * Slim shape for the members list — never the full resource with relations.
 *
 * @mixin Member
 */
class MemberRowResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'member_number' => $this->member_number,
            'full_name' => trim("{$this->first_name} {$this->last_name}"),
            'gender' => $this->gender,
            'age' => $this->date_of_birth?->age,
            'phone_primary' => $this->phone_primary,
            'branch_name' => $this->whenLoaded('branch', fn () => $this->branch?->name),
            'branch_code' => $this->whenLoaded('branch', fn () => $this->branch?->code),
            'membership_status' => $this->membership_status,
            'photo_thumb_url' => $this->photo_path ? "/api/v1/members/{$this->id}/photo?size=thumb" : null,
            'date_joined' => $this->date_joined?->toDateString(),
        ];
    }
}
