<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Infrastructure\Persistence\Eloquent;

use App\Models\User;
use Database\Factories\PublicationLogFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use ImovelZapAi\Publishing\Domain\Enums\PublicationLogEvent;
use ImovelZapAi\Publishing\Domain\Enums\PublicationLogLevel;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;

class PublicationLogModel extends Model
{
    use BelongsToTenant;
    /** @use HasFactory<PublicationLogFactory> */
    use HasFactory;
    use HasUuidPrimaryKey;

    public const UPDATED_AT = null;

    protected $table = 'publication_logs';

    /** @var list<string> */
    protected $fillable = [
        'tenant_id',
        'publication_target_id',
        'triggered_by',
        'level',
        'event',
        'attempt',
        'message',
        'context',
        'portal_response',
        'created_at',
    ];

    /** @return array<string, string> */
    protected function casts(): array
    {
        return [
            'level' => PublicationLogLevel::class,
            'event' => PublicationLogEvent::class,
            'attempt' => 'integer',
            'context' => 'array',
            'portal_response' => 'array',
            'created_at' => 'datetime',
        ];
    }

    public function target(): BelongsTo
    {
        return $this->belongsTo(PublicationTargetModel::class, 'publication_target_id');
    }

    public function triggeredBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'triggered_by');
    }

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