<?php

namespace App\Models\HR;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;

class Position extends Model
{
    use ModelTrait;

    protected $table = 'positions';

    protected $fillable = [
        'tenant_id',
        'type',
        'key',
        'name',
        'sort_order',
        'is_active',
    ];

    protected $casts = [
        'tenant_id' => 'int',
        'sort_order' => 'int',
        'is_active' => 'bool',
    ];

    public const TYPE_RANK = 'rank';

    public const TYPE_TITLE = 'title';

    // =========================================================================
    // 스코프
    // =========================================================================

    public function scopeForTenant($query, ?int $tenantId = null)
    {
        $tenantId = $tenantId ?? session('selected_tenant_id');
        if ($tenantId) {
            return $query->where('tenant_id', $tenantId);
        }

        return $query;
    }

    public function scopeRanks($query)
    {
        return $query->where('type', self::TYPE_RANK);
    }

    public function scopeTitles($query)
    {
        return $query->where('type', self::TYPE_TITLE);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }
}
