<?php

declare(strict_types=1);

namespace App\Models;

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

class DberpIntegrationMapping extends Model
{
    protected $table = 'dberp_integration_mappings';

    protected $fillable = [
        'source_system',
        'target_system',
        'object_type',
        'local_model_type',
        'local_model_id',
        'external_id',
        'external_code',
        'external_name',
        'external_parent_id',
        'external_sku',
        'external_spec',
        'status',
        'match_strategy',
        'governance_note',
        'last_error_type',
        'last_error_message',
        'source_document_no',
        'mapping_payload',
        'last_synced_at',
        'invalidated_at',
    ];

    protected function casts(): array
    {
        return [
            'local_model_id' => 'integer',
            'mapping_payload' => 'array',
            'last_synced_at' => 'datetime',
            'invalidated_at' => 'datetime',
        ];
    }

    public function localModel(): MorphTo
    {
        return $this->morphTo(__FUNCTION__, 'local_model_type', 'local_model_id');
    }
}