<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use App\Models\Evento;
use App\Models\QrCode;
use Illuminate\Support\Facades\Artisan;

class CleanupTest extends TestCase
{
    use DatabaseTransactions;

    public function test_cleanup_only_removes_qrs_but_keeps_event()
    {
        // 1. Create an old event
        $idEvento = 'old_evt_1';
        Evento::create([
            'id_evento'     => $idEvento,
            'nombre_evento' => 'Old Event',
            'fecha_evento'  => now()->subDays(40)->format('Y-m-d'),
            'hora_inicio'   => '10:00',
            'hora_fin'      => '12:00'
        ]);

        // 2. Create a QR for this event
        QrCode::create([
            'id_qr'     => 'qr_old_1',
            'id_evento' => $idEvento,
            'tipo_qr'   => 'publico'
        ]);

        // 3. Ensure they exist
        $this->assertDatabaseHas('eventos', ['id_evento' => $idEvento]);
        $this->assertDatabaseHas('qr_codes', ['id_qr' => 'qr_old_1']);

        // 4. Run the cleanup command
        Artisan::call('app:cleanup-old-events');

        // 5. Verify QRs are gone but event remains
        $this->assertDatabaseMissing('qr_codes', ['id_qr' => 'qr_old_1']);
        $this->assertDatabaseHas('eventos', ['id_evento' => $idEvento]);
    }
}
