<?php

namespace Tests\Feature\Logging;

use App\Domains\Logging\Enums\ValueChangeActorType;
use App\Domains\Logging\Models\Concerns\LogsValueChanges;
use App\Domains\Logging\Models\ValueChange;
use App\Domains\Purchase\Models\Purchase;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class LogsValueChangesTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        config()->set('database.default', 'logging_test');
        config()->set('database.connections.logging_test', [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'foreign_key_constraints' => true,
        ]);

        Schema::create('users', function (Blueprint $table): void {
            $table->id();
        });

        Schema::create('tenants', function (Blueprint $table): void {
            $table->id();
            $table->string('codigo')->unique();
        });

        Schema::getConnection()->table('tenants')->insert(['codigo' => 'test']);

        Schema::create('logging_test_products', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->unsignedInteger('price');
            $table->text('description')->nullable();
            $table->timestamps();
        });

        Schema::create('compras', function (Blueprint $table): void {
            $table->id();
            $table->string('tenant_codigo');
            $table->string('status')->default(Purchase::STATUS_CREATED);
            $table->timestamps();
        });

        $migration = require database_path('migrations/2026_08_03_000200_create_value_changes_table.php');
        $migration->up();
        $tenantMigration = require database_path('migrations/2026_08_04_000000_add_tenant_code_to_value_changes_table.php');
        $tenantMigration->up();
    }

    public function test_it_creates_one_system_record_per_configured_change(): void
    {
        $product = LoggingTestProduct::create([
            'name' => 'Original',
            'price' => 100,
            'description' => 'Old description',
        ]);

        $product->update([
            'name' => 'Updated',
            'price' => 150,
            'description' => 'New description',
        ]);

        $this->assertDatabaseCount('value_changes', 2);
        $this->assertDatabaseHas('value_changes', [
            'tenant_code' => 'test',
            'attribute' => 'name',
            'old_value' => 'Original',
            'new_value' => 'Updated',
            'actor_type' => ValueChangeActorType::System->value,
            'user_id' => null,
        ]);
        $this->assertDatabaseHas('value_changes', [
            'attribute' => 'price',
            'old_value' => '100',
            'new_value' => '150',
            'actor_type' => ValueChangeActorType::System->value,
            'user_id' => null,
        ]);
        $this->assertTrue(ValueChange::firstOrFail()->trackable->is($product));
    }

    public function test_it_associates_an_authenticated_user_with_the_change(): void
    {
        Schema::getConnection()->table('users')->insert(['id' => 7]);
        Auth::shouldReceive('id')->once()->andReturn(7);

        $product = LoggingTestProduct::create([
            'name' => 'Original',
            'price' => 100,
        ]);

        $product->update(['price' => 200]);

        $change = ValueChange::firstOrFail();

        $this->assertSame(ValueChangeActorType::User, $change->actor_type);
        $this->assertSame(7, $change->user_id);
    }

    public function test_it_does_not_log_updates_to_unconfigured_attributes(): void
    {
        $product = LoggingTestProduct::create([
            'name' => 'Original',
            'price' => 100,
            'description' => 'Old description',
        ]);

        $product->update(['description' => 'New description']);

        $this->assertDatabaseCount('value_changes', 0);
    }

    public function test_purchase_logs_its_status_changes(): void
    {
        $purchase = Purchase::query()->create([
            'tenant_codigo' => 'test',
            'status' => Purchase::STATUS_CREATED,
        ]);

        $purchase->update([
            'status' => Purchase::STATUS_PENDING_PAYMENT,
        ]);

        $this->assertDatabaseHas('value_changes', [
            'tenant_code' => 'test',
            'trackable_type' => $purchase->getMorphClass(),
            'trackable_id' => $purchase->id,
            'attribute' => 'status',
            'old_value' => Purchase::STATUS_CREATED,
            'new_value' => Purchase::STATUS_PENDING_PAYMENT,
            'actor_type' => ValueChangeActorType::System->value,
            'user_id' => null,
        ]);
    }
}

#[Fillable(['name', 'price', 'description'])]
class LoggingTestProduct extends Model
{
    use LogsValueChanges;

    protected $table = 'logging_test_products';

    /** @var array<int, string> */
    protected array $loggedAttributes = [
        'name',
        'price',
    ];

    protected function valueChangeTenantCode(): string
    {
        return 'test';
    }
}
