<?php

use App\Livewire\BulkOperationProgress;
use App\Models\BackupSchedule;
use App\Models\BackupScheduleRun;
use App\Models\BulkOperationRun;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

test('progress widget shows running operations for current tenant and user', function () {
    $tenant = Tenant::factory()->create();
    $tenant->makeCurrent();
    $user = User::factory()->create();

    // Own running op
    BulkOperationRun::factory()->create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'status' => 'running',
        'resource' => 'policy',
        'action' => 'delete',
        'total_items' => 100,
        'processed_items' => 50,
    ]);

    // Completed op (should not show)
    BulkOperationRun::factory()->create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'status' => 'completed',
        'updated_at' => now()->subMinutes(5),
    ]);

    // Other user's op (should not show)
    $otherUser = User::factory()->create();
    BulkOperationRun::factory()->create([
        'tenant_id' => $tenant->id,
        'user_id' => $otherUser->id,
        'status' => 'running',
    ]);

    auth()->login($user); // Login user explicitly for auth()->id() call in component

    Livewire::actingAs($user)
        ->test(BulkOperationProgress::class)
        ->assertSee('Delete Policy')
        ->assertSee('50 / 100');
});

test('progress widget reconciles stale pending backup schedule runs', function () {
    $tenant = Tenant::factory()->create();
    $tenant->makeCurrent();
    $user = User::factory()->create();

    $schedule = BackupSchedule::query()->create([
        'tenant_id' => $tenant->id,
        'name' => 'Nightly',
        'is_enabled' => true,
        'timezone' => 'UTC',
        'frequency' => 'daily',
        'time_of_day' => '01:00:00',
        'days_of_week' => null,
        'policy_types' => ['deviceConfiguration'],
        'include_foundations' => true,
        'retention_keep_last' => 30,
        'next_run_at' => now()->addHour(),
    ]);

    $bulkRun = BulkOperationRun::factory()->create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'status' => 'pending',
        'resource' => 'backup_schedule',
        'action' => 'run',
        'total_items' => 1,
        'processed_items' => 0,
        'item_ids' => [(string) $schedule->id],
        'created_at' => now()->subMinutes(2),
        'updated_at' => now()->subMinutes(2),
    ]);

    BackupScheduleRun::query()->create([
        'backup_schedule_id' => $schedule->id,
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'scheduled_for' => now()->startOfMinute(),
        'started_at' => now()->subMinute(),
        'finished_at' => now(),
        'status' => BackupScheduleRun::STATUS_SUCCESS,
        'summary' => null,
    ]);

    auth()->login($user);

    Livewire::actingAs($user)
        ->test(BulkOperationProgress::class)
        ->assertSee('Run Backup schedule')
        ->assertSee('1 / 1');

    expect($bulkRun->refresh()->status)->toBe('completed');
});
