<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

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

    protected $fillable = [
        'name',
        'mobile',
        'is_admin',
        'email',
        'password',
    ];

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

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

    protected $appends = [
        'profile_photo_url',
    ];

    public function subscribePlans()
    {
        return $this->hasMany(UserSubscriber::class);
    }

    public function isAdmin()
    {
        return $this->is_admin;
    }

    public function isSubscriber()
    {
        return $this->subscribePlans()->where('expired_at', '>', now())->exists();
    }

    public function subscribePlan()
    {
        return $this->subscribePlans()->where('expired_at', '>', now())->first();
    }

    public function userSubscribers()
    {
        return $this->hasMany(UserSubscriber::class);
    }

    public function notifications()
    {
        return $this->belongsToMany(Notification::class, 'notification_user')
            ->withPivot('read_at')
            ->withTimestamps();
    }
}
