<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use HasFactory;
    use Notifiable;

    protected $fillable = [
        'name',
        'business_name',
        'email',
        'password',
        'role',
        'client_type',
        'assigned_designer_id',
        'phone',
        'status',
        'notes',
        'avatar_path',
        'profile_image',
        'bio',
    ];

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

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

    public function assignedTasks()
    {
        return $this->hasMany(Task::class, 'designer_id');
    }

    public function clientTasks()
    {
        return $this->hasMany(Task::class, 'client_id');
    }

    public function assignedDesigner()
    {
        return $this->belongsTo(self::class, 'assigned_designer_id');
    }

    public function gtsWallet()
    {
        return $this->hasOne(GtsWallet::class);
    }

    public function gtsOrders()
    {
        return $this->hasMany(GtsOrder::class);
    }

    public function portalRole(): string
    {
        if ($this->isAdminRole()) {
            return 'admin';
        }

        return $this->role === 'gts_client' ? 'gts-client' : $this->role;
    }

    public function isAdminRole(): bool
    {
        return in_array($this->role, ['admin', 'super_admin'], true);
    }
}
