<?php

namespace App\Models\Tenants;

use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 직급/직책 통합 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property string $type rank(직급) | title(직책)
 * @property string|null $key 영문 키 (tenant_user_profiles 연동용)
 * @property string $name 명칭
 * @property int $sort_order 정렬 순서
 * @property bool $is_active 활성화 여부
 */
class Position extends Model
{
    use Auditable, BelongsToTenant, ModelTrait, SoftDeletes;

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

    protected $hidden = [
        'deleted_at',
    ];

    // =========================================================================
    // 상수
    // =========================================================================

    public const TYPE_RANK = 'rank';   // 직급

    public const TYPE_TITLE = 'title'; // 직책

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

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

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

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

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