<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
 * One row per (probe location, target, check time). Used both for live
 * consensus calculation (last 5 minutes' rows) and for historical uptime
 * reporting until rolled up into uptime_buckets.
 */
class LocationCheckResult extends Model
{
    use HasFactory;

    protected $fillable = [
        'probe_location_id', 'target_type', 'target_id',
        'checked_at', 'is_up', 'status_code', 'response_time_ms', 'error',
    ];

    protected function casts(): array
    {
        return [
            'checked_at' => 'datetime',
            'is_up' => 'boolean',
        ];
    }

    public function location(): BelongsTo
    {
        return $this->belongsTo(ProbeLocation::class, 'probe_location_id');
    }

    public function target(): MorphTo
    {
        return $this->morphTo();
    }
}
