<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name', 'email', 'password', 'role', 'permissions', 'phone',
        'city', 'state', 'lat', 'lng',
        'is_active', 'is_verified', 'avatar',
    ];

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

    protected $casts = [
        'email_verified_at' => 'datetime',
        'password'          => 'hashed',
        'is_active'         => 'boolean',
        'is_verified'       => 'boolean',
        'lat'               => 'float',
        'lng'               => 'float',
        'permissions'       => 'array',
    ];

    /* ── Helpers ── */
    public function isAdmin(): bool   { return $this->role === 'admin'; }
    public function isStaff(): bool   { return $this->role === 'staff'; }
    public function isEscort(): bool  { return $this->role === 'escort'; }

    public function hasPermission(string $permission): bool
    {
        if ($this->isAdmin()) return true;
        if (!$this->isStaff()) return false;
        return in_array($permission, $this->permissions ?? []);
    }

    /* ── Relations ── */
    public function listings(): HasMany
    {
        return $this->hasMany(Listing::class);
    }

    public function wallet(): HasOne
    {
        return $this->hasOne(Wallet::class);
    }

    public function walletTransactions(): HasMany
    {
        return $this->hasMany(WalletTransaction::class);
    }

    public function boosts(): HasMany
    {
        return $this->hasMany(Boost::class);
    }
}
