<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'nip',
        'rank',
        'phone',
        'email',
        'is_active'
    ];

    protected $casts = [
        'is_active' => 'boolean',
    ];

    public function assignments()
    {
        return $this->hasMany(EmployeeAssignment::class);
    }

    public function activeAssignment()
    {
        return $this->hasOne(EmployeeAssignment::class)
            ->where('is_active', true)
            ->whereNull('end_date')
            ->latest('start_date');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'employee_id');
    }
}
