<?php

namespace Tests\Unit\Services\Users;

use App\Services\Users\InactiveUserCleanupService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class InactiveUserCleanupServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_cleanup_removes_inactive_users_and_roles(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'firstname' => 'Inactive',
            'lastname' => 'User',
            'email' => 'inactive@example.com',
            'status' => 'Inactive',
            'created_at' => now()->subMinutes(30),
        ]);

        DB::table('parents')->insert([
            'id' => 1,
            'secondparent_firstname' => 'Second',
            'secondparent_lastname' => 'Parent',
            'secondparent_gender' => 'Female',
            'secondparent_email' => 'second@example.com',
            'secondparent_phone' => '1234567890',
            'firstparent_id' => 1,
            'secondparent_id' => 2,
            'semester' => 'Spring',
            'school_year' => '2024-2025',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => 1,
            'role_id' => 1,
        ]);

        $service = new InactiveUserCleanupService();
        $result = $service->cleanup(15);

        $this->assertSame(1, $result['deleted_users']);
        $this->assertDatabaseMissing('users', ['id' => 1]);
        $this->assertDatabaseMissing('parents', ['firstparent_id' => 1]);
        $this->assertDatabaseMissing('user_roles', ['user_id' => 1]);
    }
}
