<?php

use App\Enums\AuditAction;
use App\Models\AuditLog;
use App\Models\Group;
use App\Models\Household;

test('creating a household writes a created audit log', function () {
    $household = Household::factory()->create();

    $log = AuditLog::where('auditable_type', Household::class)
        ->where('auditable_id', $household->id)
        ->where('action', AuditAction::Created)
        ->first();

    expect($log)->not->toBeNull();
});

test('updating a group writes an updated audit log with only changed keys', function () {
    $group = Group::factory()->create(['name' => 'Choir']);

    $group->update(['name' => 'Worship Team']);

    $log = AuditLog::where('auditable_type', Group::class)
        ->where('auditable_id', $group->id)
        ->where('action', AuditAction::Updated)
        ->first();

    expect($log)->not->toBeNull();
    expect($log->new_values)->toHaveKey('name', 'Worship Team');
});

test('soft-deleting a household writes a deleted audit log', function () {
    $household = Household::factory()->create();

    $household->delete();

    $log = AuditLog::where('auditable_type', Household::class)
        ->where('auditable_id', $household->id)
        ->where('action', AuditAction::Deleted)
        ->first();

    expect($log)->not->toBeNull();
});
