<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Filters\UserFilters;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, SoftDeletes;

    protected string $default_filters = UserFilters::class;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        "id_prefix",
        "id_no",
        "first_name",
        "middle_name",
        "last_name",
        "suffix",
        "position_name",
        "mobile_no",
        "one_charging_id",
        "one_charging_sync_id",
        "one_charging_code",
        "one_charging_name",
        "company_id",
        "company_code",
        "company_name",
        "business_unit_id",
        "business_unit_code",
        "business_unit_name",
        "department_id",
        "department_code",
        "department_name",
        "department_unit_id",
        "department_unit_code",
        "department_unit_name",
        "sub_unit_id",
        "sub_unit_code",
        "sub_unit_name",
        "location_id",
        "location_code",
        "location_name",
        "warehouse_id",
        "username",
        "password",
        "role_id",
        "farm_id",
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        "password",
        "created_at",
        "company_id",
        "business_unit_id",
        "department_id",
        "department_unit_id",
        "sub_unit_id",
        "location_id",
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        "email_verified_at" => "datetime",
        "password" => "hashed",
    ];

    public function role()
    {
        return $this->belongsTo(Role::class, "role_id", "id")->withTrashed();
    }

    public function farm()
    {
        return $this->belongsTo(Farm::class, "farm_id", "id")->withTrashed();
    }

    public function warehouse()
    {
        return $this->belongsTo(
            Warehouse::class,
            "warehouse_id",
            "id"
        )->withTrashed();
    }

    public function setFarmIdAttribute($value): void
    {
        $this->attributes["farm_id"] = $value ? (int) $value : null;
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->whereNull("deleted_at");
    }

    public function hasPermission(string $permission): bool
    {
        if (!$this->role || !$this->role->access_permission) {
            return false;
        }

        $permissions = array_map('trim', explode(',', $this->role->access_permission));

        return in_array($permission, $permissions);
    }
}
