<?php

declare(strict_types=1);

namespace App\Models;

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

class DberpIntegrationSyncLog extends Model
{
    protected $table = 'dberp_integration_sync_logs';

    protected $fillable = [
        'channel',
        'direction',
        'status',
        'sync_object_type',
        'source_system',
        'target_system',
        'reference',
        'source_document_no',
        'external_reference',
        'message',
        'error_type',
        'result_summary',
        'context',
        'related_model_type',
        'related_model_id',
        'retry_count',
        'latest_retry_at',
        'latest_retry_status',
        'latest_retry_message',
    ];

    protected function casts(): array
    {
        return [
            'context' => 'array',
            'created_at' => 'datetime',
            'updated_at' => 'datetime',
            'related_model_id' => 'integer',
            'retry_count' => 'integer',
            'latest_retry_at' => 'datetime',
        ];
    }

    public function relatedModel(): MorphTo
    {
        return $this->morphTo(__FUNCTION__, 'related_model_type', 'related_model_id');
    }
}
