<?php

namespace App\Http\Resources\User;

use App\Http\Resources\File\StoredFileResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

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,
            'username' => $this->username,
            'email' => $this->email,
            'whatsapp_number' => $this->whatsapp_number,
            'profile_photo' => $this->whenLoaded(
                'profilePhoto',
                fn () => $this->profilePhoto ? new StoredFileResource($this->profilePhoto) : null
            ),
            'is_master' => (bool) $this->is_master,
            'access_level' => $this->access_level->value,
            'status' => $this->status,
            'verification_channel' => $this->verification_channel,
            'verification_target' => $this->verification_target,
            'verified_at' => $this->verified_at,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

            'user_profile' => $this->whenLoaded('userProfile', function () {
                return $this->userProfile ? [
                    'id' => $this->userProfile->id,
                    'user_id' => $this->userProfile->user_id,
                    'nik' => $this->userProfile->nik,
                    'address' => $this->userProfile->address,
                    'provinsi_id' => $this->userProfile->provinsi_id,
                    'kabupaten_id' => $this->userProfile->kabupaten_id,
                    'kecamatan_id' => $this->userProfile->kecamatan_id,
                    'desa_id' => $this->userProfile->desa_id,
                    'created_at' => $this->userProfile->created_at,
                    'updated_at' => $this->userProfile->updated_at,
                ] : null;
            }),

            'user_tenants' => $this->whenLoaded('userTenants', function () {
                return $this->userTenants->map(function ($item) {
                    return [
                        'id' => $item->id,
                        'user_id' => $item->user_id,
                        'tenant_id' => $item->tenant_id,
                        'role' => $item->role,
                        'is_default' => (bool) $item->is_default,
                        'created_at' => $item->created_at,
                        'updated_at' => $item->updated_at,
                        'tenant' => $item->relationLoaded('tenant') && $item->tenant ? [
                            'id' => $item->tenant->id,
                            'tenant_code' => $item->tenant->tenant_code ?? null,
                            'tenant_name' => $item->tenant->tenant_name ?? null,
                        ] : null,
                    ];
                })->values();
            }),

            'user_menu_groups' => $this->whenLoaded('userMenuGroups', function () {
                return $this->userMenuGroups->map(function ($item) {
                    return [
                        'id' => $item->id,
                        'user_id' => $item->user_id,
                        'menu_group_id' => $item->menu_group_id,
                        'tenant_id' => $item->tenant_id,
                        'created_at' => $item->created_at,
                        'updated_at' => $item->updated_at,
                        'menu_group' => $item->relationLoaded('menuGroup') && $item->menuGroup ? [
                            'id' => $item->menuGroup->id,
                            'name' => $item->menuGroup->name ?? null,
                            'menu_group_name' => $item->menuGroup->menu_group_name ?? null,
                        ] : null,
                        'tenant' => $item->relationLoaded('tenant') && $item->tenant ? [
                            'id' => $item->tenant->id,
                            'tenant_code' => $item->tenant->tenant_code ?? null,
                            'tenant_name' => $item->tenant->tenant_name ?? null,
                        ] : null,
                    ];
                })->values();
            }),
        ];
    }
}
