<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
        'user_type',
        'is_active',
    ];

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

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

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [
            'user_type' => $this->user_type,
        ];
    }

    public function supplier()
    {
        return $this->hasOne(Supplier::class);
    }

    public function company()
    {
        return $this->hasOne(Company::class);
    }

    public function isSupplier()
    {
        return $this->user_type === 'supplier';
    }

    public function isCompany()
    {
        return $this->user_type === 'company';
    }

    public function isAdmin()
    {
        return $this->user_type === 'admin';
    }
}
