<?php

namespace Tests\Feature\Console;

use App\Services\Auth\PasswordResetCleanupService;
use App\Services\Notifications\NotificationCleanupService;
use App\Services\System\ConfigUpdateService;
use App\Services\Users\InactiveUserCleanupService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class SystemCommandsTest extends TestCase
{
    use RefreshDatabase;

    public function test_cleanup_expired_notifications_command_runs(): void
    {
        $service = Mockery::mock(NotificationCleanupService::class);
        $service->shouldReceive('cleanupExpired')->once()->andReturn(2);

        $this->app->instance(NotificationCleanupService::class, $service);

        $this->artisan('notifications:cleanup')->assertExitCode(0);
    }

    public function test_cleanup_password_resets_command_runs(): void
    {
        $service = Mockery::mock(PasswordResetCleanupService::class);
        $service->shouldReceive('cleanup')->once()->andReturn(1);

        $this->app->instance(PasswordResetCleanupService::class, $service);

        $this->artisan('cleanup:password-resets')->assertExitCode(0);
    }

    public function test_config_update_command_runs(): void
    {
        $service = Mockery::mock(ConfigUpdateService::class);
        $service->shouldReceive('availableTasks')->andReturn(['enable_attendance_on']);
        $service->shouldReceive('runTask')->once()->andReturn(['ok' => true]);

        $this->app->instance(ConfigUpdateService::class, $service);

        $this->artisan('config:update', ['--task' => 'enable_attendance_on', '--tz' => 'UTC'])->assertExitCode(0);
    }

    public function test_delete_inactive_users_command_runs(): void
    {
        $service = Mockery::mock(InactiveUserCleanupService::class);
        $service->shouldReceive('cleanup')->once()->andReturn([
            'deleted_users' => 0,
            'deleted_parents' => 0,
            'deleted_roles' => 0,
        ]);

        $this->app->instance(InactiveUserCleanupService::class, $service);

        $this->artisan('users:delete-inactive-users')->assertExitCode(0);
    }
}
