<?php

declare(strict_types=1);

namespace App\Http\Resources;

use App\Models\User;
use App\Support\FileUrl;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $roles = $this->relationLoaded('roles')
            ? $this->roles->pluck('name')->values()
            : $this->getRoleNames()->values();

        $accountType = $this->resolveAccountType();
        $primaryRole = $this->primaryRole();

        return [
            'id' => $this->id,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'name' => $this->username ?: $this->name,
            'username' => $this->username,
            'full_name' => trim(($this->first_name ?? '') . ' ' . ($this->last_name ?? '')) ?: $this->name,
            'national_code' => $this->national_code,
            'national_cart_image' => FileUrl::url($this->national_cart_image),
            'cellphone' => $this->cellphone,
            'email' => $this->email,
            'dob' => $this->dob?->toDateString(),
            'phone' => $this->phone,
            'address' => $this->address,
            'province_id' => $this->province_id,
            'county_id' => $this->county_id,
            'postal_code' => $this->postal_code,
            'latitude' => $this->latitude !== null ? (float) $this->latitude : null,
            'longitude' => $this->longitude !== null ? (float) $this->longitude : null,
            'gender' => $this->gender,
            'wallet' => $this->wallet !== null ? (int) $this->wallet : null,
            'balance' => $this->wallet !== null ? (int) $this->wallet : null,
            'avatar' => FileUrl::url($this->avatar),
            'desc' => $this->desc,
            'status' => (int) $this->status,
            'is_blocked' => (bool) $this->is_blocked,
            'blocked_reason' => $this->blocked_reason,
            'blocked_at' => $this->blocked_at?->toISOString(),
            'account_type' => $accountType,
            'account_type_label' => User::accountTypeOptions()[$accountType] ?? $accountType,
            'role' => $primaryRole,
            'role_label' => $primaryRole ? (User::roleOptions()[$primaryRole] ?? $primaryRole) : null,
            'roles' => $roles,
            'producer_id' => $this->whenLoaded('producer', fn () => $this->producer?->id),
            'expert_id' => $this->whenLoaded('expert', fn () => $this->expert?->id),
            'producer_status' => $this->whenLoaded('producer', fn () => $this->producer?->status),
            'expert_status' => $this->whenLoaded('expert', fn () => $this->expert?->status),
            'producer' => new ProducerResource($this->whenLoaded('producer')),
            'expert' => new ExpertResource($this->whenLoaded('expert')),
            'province' => $this->whenLoaded('province', fn () => $this->province?->only(['id', 'name'])),
            'county' => $this->whenLoaded('county', fn () => $this->county?->only(['id', 'name'])),
            'created_at' => $this->created_at?->toISOString(),
        ];
    }
}
