<?php

namespace App\Models;

use App\Enums\Status;
use Illuminate\Database\Eloquent\Builder;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\HasMany;

class UserGroup extends BaseModel
{
    protected $table = 'user_groups';

    public const CREATED_AT = 'create_time';

    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'name',
        'code',
        'description',
        'icon',
        'color',
        'is_default',
        'sort',
        'status',
    ];

    protected $casts = [
        'status' => Status::class,
        'is_default' => 'integer',
    ];

    public function users(): HasMany
    {
        return $this->hasMany(User::class, 'group_id');
    }

    public function scopeDefault(Builder $query): Builder
    {
        return $query->where('is_default', 1);
    }

    public function scopeEnabled(Builder $query): Builder
    {
        return $query->where('status', Status::ENABLED);
    }

    public function hasUsers(): bool
    {
        return $this->users()->exists();
    }
}
