<?php
/*
namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use NotificationChannels\Telegram\TelegramChannel;
*/
/**
 * @method static where(string $string, string $string1, int|string|null $currentUserId)
 * @method static select(string $string, string $string1, string $string2)
 * @method static create(array $validated)
 * @property mixed $role
 * @property false|mixed $is_online
 * @property mixed $id
 */
/*
class User extends Authenticatable
{*/
    /** @use HasFactory<\Database\Factories\UserFactory> */
    //use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
/*
    protected $casts = [
        'last_seen' => 'datetime',
    ];

    protected $appends = ['is_online'];

    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'rating',
        'telegram_chat_id',
        'expo_push_token',
        'city',
        'last_seen'
    ];
*/
    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    /*
    protected $hidden = [
        'password',
        'remember_token',
    ];*/

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    /*
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'last_activity_at' => 'datetime',
            'is_online' => 'boolean',
        ];
    }

    public function createdRequests(): User|HasMany
    {
        return $this->hasMany(InstallRequest::class, 'created_by');
    }

    public function assignedRequests(): User|HasMany
    {
        return $this->hasMany(InstallRequest::class, 'assigned_to');
    }

    public function actions(): User|HasMany
    {
        return $this->hasMany(InstallRequestAction::class);
    }

    public function receivedRatings(): User|HasMany
    {
        return $this->hasMany(RequestRating::class, 'rated_user_id');
    }

    public function givenRatings(): User|HasMany
    {
        return $this->hasMany(RequestRating::class, 'rated_by');
    }

    // Связь с заявками, назначенными установщику
    public function installRequests(): HasMany
    {
        return $this->hasMany(InstallRequest::class, 'assigned_to');
    }

    // Связь с рейтингами, которые получил установщик (через модель RequestRating)
    public function ratingsReceived(): User|HasMany
    {
        return $this->hasMany(RequestRating::class, 'rated_user_id');
    }

    public function routeNotificationForTelegram()
    {
        return $this->telegram_chat_id;
    }

    public function installerConsumables(): User|HasMany
    {
        return $this->hasMany(InstallerConsumable::class, 'installer_id');
    }

    public function getIsOnlineAttribute(): bool
    {
        if (!$this->last_seen) {
            return false;
        }

        return $this->last_seen->diffInSeconds(now()) < 60;
    }
}*/
namespace App\Models;

use App\Models\InstallRequest;
use App\Models\InstallRequestAction;
use App\Models\InstallerConsumable;
use App\Models\RequestRating;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
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',
        'rating',
        'telegram_chat_id',
        'expo_push_token',
        'city',
        'last_seen',
    ];

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

    protected $casts = [
        'email_verified_at' => 'datetime',
        'last_seen' => 'datetime',
        'password' => 'hashed',
    ];

    protected $appends = [
        'is_online',
    ];

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    public function createdRequests(): HasMany
    {
        return $this->hasMany(InstallRequest::class, 'created_by');
    }

    public function assignedRequests(): HasMany
    {
        return $this->hasMany(InstallRequest::class, 'assigned_to');
    }

    public function installRequests(): HasMany
    {
        return $this->hasMany(InstallRequest::class, 'assigned_to');
    }

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

    public function ratingsReceived(): HasMany
    {
        return $this->hasMany(RequestRating::class, 'rated_user_id');
    }

    public function ratingsGiven(): HasMany
    {
        return $this->hasMany(RequestRating::class, 'rated_by');
    }

    public function installerConsumables(): HasMany
    {
        return $this->hasMany(InstallerConsumable::class, 'installer_id');
    }

    public function receivedMessages(): HasMany
    {
        return $this->hasMany(Chat::class, 'receiver_id');
    }

    /*
    |--------------------------------------------------------------------------
    | NOTIFICATIONS
    |--------------------------------------------------------------------------
    */

    public function routeNotificationForTelegram(): ?string
    {
        return $this->telegram_chat_id;
    }

    /*
    |--------------------------------------------------------------------------
    | ACCESSORS
    |--------------------------------------------------------------------------
    */

    public function getIsOnlineAttribute(): bool
    {
        if (!$this->last_seen) {
            return false;
        }

        return $this->last_seen->gt(now()->subSeconds(60));
    }
}
