<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Order;
use App\Models\FoodCart;
use App\Models\FoodMenuItem;
use App\Models\Reel;
use App\Models\Shop;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * 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',
        ];
    }

    public function shops()
    {
        return $this->belongsToMany(Shop::class)->withTimestamps();
    }

    public function createdShops()
    {
        return $this->hasMany(Shop::class, 'created_by');
    }

    public function isShopStaff(Shop $shop): bool
    {
        if ($this->id === $shop->created_by) {
            return true;
        }

        return $this->shops()->where('shop_id', $shop->id)->exists();
    }

    public function isSuperAdmin(): bool
    {
        return $this->hasRole('super-admin');
    }

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

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

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

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