<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class DisciplineRecord extends Model
{
    protected $fillable = [
        'case_number','employee_id','reported_by','handled_by',
        'infraction_type','incident_date','location','description','evidence',
        'action_taken','suspension_days','action_date','action_details',
        'employee_response','status','requires_hearing','hearing_date','hearing_outcome',
    ];

    protected $casts = [
        'incident_date'   => 'date',
        'action_date'     => 'date',
        'hearing_date'    => 'date',
        'requires_hearing'=> 'boolean',
    ];

    protected static function booted(): void
    {
        static::creating(function ($d) {
            if (! $d->case_number) {
                $d->case_number = 'DC-'.date('Y').'-'.str_pad(
                    self::whereYear('created_at', date('Y'))->count() + 1,
                    4, '0', STR_PAD_LEFT
                );
            }
        });
    }

    public function getStatusBadgeAttribute(): string
    {
        return match ($this->status) {
            'open'                 => 'danger',
            'in_progress'          => 'warning',
            'resolved'             => 'secondary',
            'closed'               => 'success',
            default                => 'light',
        };
    }

    public function employee()   { return $this->belongsTo(Employee::class); }
    public function reportedBy() { return $this->belongsTo(User::class, 'reported_by'); }
    public function handledBy()  { return $this->belongsTo(User::class, 'handled_by'); }
}
