<?php

namespace Modules\Auth\Transformers;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
use Modules\User\Transformers\AddressResource;
use Modules\User\Transformers\BankDetailResource;
use Modules\User\Transformers\EmploymentResource;
use Modules\User\Transformers\HeirResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
        'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'ic_number' => $this->ic_number,
            'position' => $this->position,
            'phone_number' => $this->phone_number,
            'image_url' => $this->image_url ? Storage::disk('public')->url($this->image_url) : null,
            'status' => $this->status,
            'two_factor_secret' => $this->two_factor_secret,
            'two_factor_recovery_codes' => $this->two_factor_recovery_codes,
            'two_factor_confirmed_at' => $this->two_factor_confirmed_at,
            'email_verified_at' => $this->email_verified_at,
            'gender' => $this->gender,
            'marriage_status' => $this->marriage_status,
            'member_number' => $this->member_number,
            'member_type' => $this->member_type,
            'join_date' => $this->join_date,
            'birth_date' => $this->birth_date,
            'birth_place' => $this->birth_place,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
            'addresses' => AddressResource::collection($this->whenLoaded('addresses')),
            'employments' => EmploymentResource::collection($this->whenLoaded('employments')),
            'bank_details' => BankDetailResource::collection($this->whenLoaded('bankDetails')),
            'heirs' => HeirResource::collection($this->whenLoaded('heirs')),
            'roles' => $this->whenLoaded('roles', function () {
                return $this->roles->map(function ($role) {
                    return [
                        'id' => $role->id,
                        'name' => $role->name,
                        'fullname' => $role->fullname ?? null,
                        'context' => $role->context ?? 'member',
                        'guard_name' => $role->guard_name,
                        'permissions' => $this->when($role->relationLoaded('permissions'), function () use ($role) {
                            return $role->permissions->map(function ($permission) {
                                return [
                                    'id' => $permission->id,
                                    'name' => $permission->name,
                                    'guard_name' => $permission->guard_name,
                                    'route_name' => $permission->route_name,
                                    'created_at' => $permission->created_at,
                                    'updated_at' => $permission->updated_at,
                                ];
                            });
                        }),
                        'created_at' => $role->created_at,
                        'updated_at' => $role->updated_at,
                    ];
                });
            }),
        ];
    }
}
