<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

/**
 * @mixin Member
 */
class MemberResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'member_number' => $this->member_number,
            'branch' => $this->whenLoaded('branch', fn () => [
                'id' => $this->branch->id,
                'name' => $this->branch->name,
                'code' => $this->branch->code,
            ]),
            'title' => $this->title,
            'first_name' => $this->first_name,
            'middle_name' => $this->middle_name,
            'last_name' => $this->last_name,
            'preferred_name' => $this->preferred_name,
            'full_name' => trim("{$this->first_name} {$this->last_name}"),
            'gender' => $this->gender,
            'date_of_birth' => $this->date_of_birth?->toDateString(),
            'age' => $this->date_of_birth?->age,
            'marital_status' => $this->marital_status,
            'email' => $this->email,
            'phone_primary' => $this->phone_primary,
            'phone_secondary' => $this->phone_secondary,
            'address' => [
                'line1' => $this->address_line1,
                'line2' => $this->address_line2,
                'city' => $this->city,
                'state_province' => $this->state_province,
                'postal_code' => $this->postal_code,
                'country' => $this->country,
            ],
            'photo_thumb_url' => $this->photo_path ? "/api/v1/members/{$this->id}/photo?size=thumb" : null,
            'membership_status' => $this->membership_status,
            'date_joined' => $this->date_joined?->toDateString(),
            'date_baptized' => $this->date_baptized?->toDateString(),
            'baptism_type' => $this->baptism_type,
            'occupation' => $this->occupation,
            'employer' => $this->employer,
            'emergency_contact' => [
                'name' => $this->emergency_contact_name,
                'phone' => $this->emergency_contact_phone,
                'relationship' => $this->emergency_contact_relationship,
            ],
            'household' => $this->whenLoaded('household', fn () => $this->household ? [
                'id' => $this->household->id,
                'name' => $this->household->name,
                'role' => $this->household_role,
            ] : null),
            'groups' => $this->whenLoaded('groups', fn () => $this->groups->map(fn ($group) => [
                'id' => $group->id,
                'name' => $group->name,
                'role_in_group' => $group->pivot->role_in_group,
            ])),
            'latest_status_change' => $this->whenLoaded('latestStatusChange', fn () => $this->latestStatusChange ? [
                'from_status' => $this->latestStatusChange->from_status,
                'to_status' => $this->latestStatusChange->to_status,
                'reason' => $this->latestStatusChange->reason,
                'effective_date' => $this->latestStatusChange->effective_date?->toDateString(),
                'created_at' => $this->latestStatusChange->created_at?->toIso8601String(),
            ] : null),
            'custom_fields' => $this->custom_fields ?? [],
            'created_at' => $this->created_at?->toIso8601String(),
            'updated_at' => $this->updated_at?->toIso8601String(),
        ];
    }
}
