<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * Webhook-job упавший после 3 ретраев (см. RouteSupplierLeadJob::failed()).
 *
 * Tenant-aware с RLS. Хранит raw payload + текст исключения для ручного
 * retry из админки SaaS (`retried_at`/`retried_by` заполняются админом).
 *
 * Источник: db/schema.sql v8.7 §6, table `failed_webhook_jobs`.
 *
 * @mixin IdeHelperFailedWebhookJob
 */
class FailedWebhookJob extends Model
{
    public $timestamps = false;

    protected $table = 'failed_webhook_jobs';

    protected $fillable = [
        'tenant_id',
        'webhook_log_id',
        'raw_payload',
        'exception',
        'retry_count',
        'failed_at',
        'retried_at',
        'retried_by',
        'resolved_at',
    ];

    protected function casts(): array
    {
        return [
            'tenant_id' => 'integer',
            'webhook_log_id' => 'integer',
            'raw_payload' => 'array',
            'retry_count' => 'integer',
            'retried_by' => 'integer',
            'failed_at' => 'datetime',
            'retried_at' => 'datetime',
            'resolved_at' => 'datetime',
        ];
    }

    /** @return BelongsTo<Tenant, $this> */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class);
    }
}
