<?php

namespace App\Models;

use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable;

    protected $fillable = [
        'name', 'first_name', 'last_name', 'email', 'phone_country_code', 'phone', 'currency', 'currency_locked_at', 'currency_source', 'country',
        'google_id', 'avatar', 'provider', 'password', 'email_verified_at',
        'whmcs_client_id', 'whmcs_user_id', 'whmcs_email', 'whmcs_sync_status', 'whmcs_synced_at', 'whmcs_payload',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'currency_locked_at' => 'datetime',
            'whmcs_synced_at' => 'datetime',
            'whmcs_payload' => 'array',
            'password' => 'hashed',
        ];
    }

    public function profile()
    {
        return $this->hasOne(CustomerProfile::class);
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }


    public function displayName(): string
    {
        return trim(($this->first_name ?? '').' '.($this->last_name ?? '')) ?: $this->name;
    }
}
