<?php

namespace Tests\Tenant;

use Illuminate\Support\Facades\DB;
use Mtc\MercuryDataModels\ScheduledTask;
use Tests\TenantTestCase;

class ScheduledTaskAuditTest extends TenantTestCase
{
    public function testUpdatingATaskRecordsAnAuditUnderTheScheduledTaskMorphAlias(): void
    {
        // Auditing is skipped for console (CLI) runs by default; enable it so the test
        // exercises the same recording path a web request would.
        config(['audit.console' => true]);

        $task = ScheduledTask::factory()->create(['name' => 'Before']);

        $task->update(['name' => 'After']);

        $audit = DB::table('audits')
            ->where('auditable_type', 'scheduled-task')
            ->where('auditable_id', $task->id)
            ->where('event', 'updated')
            ->latest('id')
            ->first();

        $this->assertNotNull($audit, 'Expected an audit row for the updated scheduled task');

        $modified = json_decode($audit->new_values, true);
        $this->assertSame('After', $modified['name']);
    }
}
