<?php

declare(strict_types=1);

namespace App\Domain\Status\Models;

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

class StatusComponent extends Model
{
    protected $fillable = [
        'name',
        'slug',
        'description',
        'category',
        'check_type',
        'endpoint',
        'expected_status_code',
        'expected_response_substring',
        'check_interval_seconds',
        'timeout_seconds',
        'current_status',
        'last_check_at',
        'display_order',
        'public',
    ];

    protected $casts = [
        'public' => 'boolean',
        'last_check_at' => 'datetime',
    ];

    public function checks(): HasMany
    {
        return $this->hasMany(StatusCheck::class);
    }

    public function recentChecks(int $limit = 20): HasMany
    {
        return $this->hasMany(StatusCheck::class)->latest('checked_at')->limit($limit);
    }

    /** Human-readable status label (TR). */
    public function statusLabel(): string
    {
        return match ($this->current_status) {
            'operational' => 'Operasyonel',
            'degraded' => 'Yavaşlama',
            'partial_outage' => 'Kısmi Kesinti',
            'major_outage' => 'Büyük Kesinti',
            'maintenance' => 'Bakımda',
            default => $this->current_status,
        };
    }

    /** Tailwind color class for status badge. */
    public function statusColor(): string
    {
        return match ($this->current_status) {
            'operational' => 'green',
            'degraded' => 'yellow',
            'partial_outage' => 'orange',
            'major_outage' => 'red',
            'maintenance' => 'blue',
            default => 'gray',
        };
    }
}
