<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Cashier\Billable;
use App\Models\Review;
use App\Models\Booking;
use Illuminate\Auth\Notifications\ResetPassword;
use Lab404\Impersonate\Models\Impersonate;

class User extends \Classiebit\Eventmie\Models\User
{
    use HasApiTokens, HasFactory, Notifiable, Billable, Impersonate;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $guarded = ['id'];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Get the reviews for the user .
     */
    public function reviews()
    {
        return $this->hasMany(Review::class);
    }

    /**
     * Get the booking for the user .
     */
    public function bookings()
    {
        return $this->hasMany(Booking::class, 'customer_id', 'id');
    }

    public function sendPasswordResetNotification($token) 
    {   
        $url = route('eventmie.password.reset',[$token]);

        if(\Request::route()->getName() == 'api-password.email')
            $url = route('eventmie.password.reset',[$token]).'?api=true';

        
        // The trick is first to instantiate the notification itself
        $notification = new ResetPassword($token);
        // Then use the createUrlUsing method
        $notification->createUrlUsing(function ($token) use($url) {

            return $url;
        });

        // Then you pass the notification
        $this->notify($notification);
    }

    public function get_tags($params = [])
    {
        $user = User::find($params['organizer_id']);
        $query = $user->tags()->orderBy('updated_at', 'DESC');

        // Apply search filter on title and sub_title
        if (!empty($params['search'])) {
            $search = $params['search'];
            $query->where(function ($q) use ($search) {
                $q->where('title', 'LIKE', "%{$search}%")
                ->orWhere('sub_title', 'LIKE', "%{$search}%");
            });
        }

        // Paginate results with dynamic per_page
        return $query->paginate($params['per_page'] ?? 10);
    }

    public function hasDeviceTokens()
    {
        return !empty($this->fcm_token) || !empty($this->apn_token);
    }


   
}
