<?php

declare(strict_types=1);

namespace Kylin\Hr\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 部门模型
 *
 * 管理公司组织架构中的部门信息。
 *
 * @property string $name       部门名称
 * @property int    $parent_id  上级部门ID
 * @property int    $sort_order 排序
 */
class Department extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'parent_id',
        'name',
        'code',
        'manager_id',
        'sort',
        'status',
    ];

    protected function casts(): array
    {
        return [
            'status' => 'integer',
            'sort' => 'integer',
        ];
    }

    /**
     * 上级部门
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(static::class, 'parent_id');
    }

    /**
     * 下级部门
     */
    public function children(): HasMany
    {
        return $this->hasMany(static::class, 'parent_id');
    }

    /**
     * 部门负责人
     */
    public function manager(): BelongsTo
    {
        return $this->belongsTo(Employee::class, 'manager_id');
    }

    /**
     * 部门员工
     */
    public function employees(): HasMany
    {
        return $this->hasMany(Employee::class);
    }

    /**
     * 部门岗位
     */
    public function positions(): HasMany
    {
        return $this->hasMany(Position::class);
    }
}
