<?php

namespace App\Models;

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

/**
 * StaffAchievement Model
 *
 * Represents an achievement or accomplishment by a staff member
 * during their employment. Linked to a StaffHistory record.
 *
 * @property int $id
 * @property int $staff_history_id
 * @property string $achievement_description
 * @property \Illuminate\Support\Carbon $achievement_date
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 */
class StaffAchievement extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'staff_history_id',
        'achievement_description',
        'achievement_date',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'achievement_date' => 'date',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get the staff history record for this achievement.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function staffHistory()
    {
        return $this->belongsTo(StaffHistory::class);
    }

    /**
     * Get the staff member through the staff history.
     *
     * @return string
     */
    public function getStaffName(): string
    {
        return $this->staffHistory->staff_name;
    }

    /**
     * Get formatted achievement date.
     *
     * @return string
     */
    public function getFormattedDateAttribute(): string
    {
        return $this->achievement_date->format('F j, Y');
    }

    /**
     * Scope to order by achievement date.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $direction
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOrderByDate($query, string $direction = 'desc')
    {
        return $query->orderBy('achievement_date', $direction);
    }
}
