<?php

use App\Jobs\PruneAuditLogsJob;
use App\Models\AuditLog;
use App\Models\Member;

test('it deletes audit_logs rows older than 24 months and keeps recent ones', function () {
    $member = Member::factory()->create();

    $old = AuditLog::factory()->create([
        'auditable_type' => Member::class,
        'auditable_id' => $member->id,
        'created_at' => now()->subMonths(25),
    ]);
    $recent = AuditLog::factory()->create([
        'auditable_type' => Member::class,
        'auditable_id' => $member->id,
        'created_at' => now()->subMonths(1),
    ]);

    (new PruneAuditLogsJob)->handle();

    $this->assertDatabaseMissing('audit_logs', ['id' => $old->id]);
    $this->assertDatabaseHas('audit_logs', ['id' => $recent->id]);
});

test('it processes more than one chunk when needed', function () {
    $member = Member::factory()->create();

    AuditLog::factory()->count(3)->create([
        'auditable_type' => Member::class,
        'auditable_id' => $member->id,
        'created_at' => now()->subMonths(30),
    ]);

    (new PruneAuditLogsJob)->handle();

    expect(AuditLog::where('created_at', '<', now()->subMonths(24))->count())->toBe(0);
});
