<?php

namespace Modules\Vrc\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class VrcSafetyTraining extends Model
{
    protected $table = 'vrc_safety_trainings';

    protected $fillable = [
        'business_id', 'technician_id', 'user_id', 'course', 'provider',
        'completed_on', 'expires_on', 'certificate_path', 'status', 'notes',
    ];

    protected $casts = [
        'completed_on' => 'date',
        'expires_on'   => 'date',
    ];

    public function technician(): BelongsTo
    {
        return $this->belongsTo(VrcTechnician::class, 'technician_id');
    }

    public function computedStatus(): string
    {
        if (! $this->expires_on) {
            return 'valid';
        }
        $days = now()->startOfDay()->diffInDays($this->expires_on, false);
        if ($days < 0) {
            return 'expired';
        }
        return $days <= 30 ? 'expiring' : 'valid';
    }
}
