<?php

declare(strict_types=1);

namespace ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent;

use Database\Factories\ImportLogFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use ImovelZapAi\Importing\Domain\Enums\ImportLogEvent;
use ImovelZapAi\Properties\Infrastructure\Persistence\Eloquent\PropertyModel;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;

class ImportLogModel extends Model
{
    use BelongsToTenant;
    use HasFactory;
    use HasUuidPrimaryKey;

    public $timestamps = false;

    protected $table = 'import_logs';

    protected $fillable = [
        'tenant_id',
        'import_job_id',
        'property_id',
        'external_id',
        'listing_index',
        'event',
        'result',
        'diff_before',
        'diff_after',
        'photos_count',
        'duration_ms',
        'message',
        'context',
        'created_at',
    ];

    protected function casts(): array
    {
        return [
            'event' => ImportLogEvent::class,
            'diff_before' => 'array',
            'diff_after' => 'array',
            'context' => 'array',
            'created_at' => 'datetime',
            'listing_index' => 'integer',
            'photos_count' => 'integer',
            'duration_ms' => 'integer',
        ];
    }

    protected static function newFactory(): ImportLogFactory
    {
        return ImportLogFactory::new();
    }

    public function job(): BelongsTo
    {
        return $this->belongsTo(ImportJobModel::class, 'import_job_id');
    }

    public function property(): BelongsTo
    {
        return $this->belongsTo(PropertyModel::class, 'property_id');
    }
}
