<?php

declare(strict_types=1);

namespace ImovelZapAi\Visits\Infrastructure\Persistence\Eloquent;

use App\Models\User;
use Database\Factories\VisitLogFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;
use ImovelZapAi\Visits\Domain\Enums\VisitLogEvent;

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

    public $timestamps = false;

    protected $table = 'visit_logs';

    protected $fillable = [
        'tenant_id',
        'visit_id',
        'actor_user_id',
        'event',
        'before',
        'after',
        'message',
        'created_at',
    ];

    protected function casts(): array
    {
        return [
            'event' => VisitLogEvent::class,
            'before' => 'array',
            'after' => 'array',
            'created_at' => 'datetime',
        ];
    }

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

    public function visit(): BelongsTo
    {
        return $this->belongsTo(VisitModel::class, 'visit_id');
    }

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