<?php

namespace Tests\Unit\Logging;

use App\Domains\Auth\Models\User;
use App\Domains\Logging\Enums\ValueChangeActorType;
use App\Domains\Logging\Models\ValueChange;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Tests\TestCase;

class ValueChangeTest extends TestCase
{
    public function test_value_change_maps_its_values_and_relations(): void
    {
        $valueChange = new ValueChange;
        $valueChange->setRawAttributes([
            'tenant_code' => 'test',
            'trackable_id' => '10',
            'attribute' => 'status',
            'old_value' => 'pending',
            'new_value' => 'paid',
            'changed_at' => '2026-08-03 15:30:00',
            'actor_type' => ValueChangeActorType::User->value,
            'user_id' => '20',
        ]);

        $this->assertSame('value_changes', $valueChange->getTable());
        $this->assertFalse($valueChange->usesTimestamps());
        $this->assertSame('test', $valueChange->tenant_code);
        $this->assertSame(10, $valueChange->trackable_id);
        $this->assertSame('status', $valueChange->attribute);
        $this->assertSame('pending', $valueChange->old_value);
        $this->assertSame('paid', $valueChange->new_value);
        $this->assertSame('2026-08-03 15:30:00', $valueChange->changed_at->format('Y-m-d H:i:s'));
        $this->assertSame(ValueChangeActorType::User, $valueChange->actor_type);
        $this->assertSame(20, $valueChange->user_id);
        $this->assertInstanceOf(MorphTo::class, $valueChange->trackable());
        $this->assertInstanceOf(User::class, $valueChange->user()->getRelated());
        $this->assertInstanceOf(Tenant::class, $valueChange->tenant()->getRelated());
    }
}
