<?php

namespace Tests\Feature\Console;

use App\Models\Event;
use App\Models\EventMediaAsset;
use App\Models\MediaStorageTarget;
use App\Services\Storage\StorageHealthService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Mockery;
use Tests\TestCase;

class MonitorStorageCommandTest extends TestCase
{
    use RefreshDatabase;

    public function test_monitor_command_caches_capacity_snapshot(): void
    {
        $target = MediaStorageTarget::create([
            'key' => 'local-ssd',
            'name' => 'Local SSD',
            'driver' => 'local',
            'config' => ['monitor_path' => storage_path('app')],
            'is_hot' => true,
            'is_default' => true,
            'is_active' => true,
            'priority' => 100,
        ]);

        $event = Event::factory()->create();

        EventMediaAsset::create([
            'event_id' => $event->id,
            'media_storage_target_id' => $target->id,
            'photo_id' => null,
            'variant' => 'original',
            'disk' => 'local-ssd',
            'path' => 'events/'.$event->id.'/photo.jpg',
            'size_bytes' => 2048,
            'status' => 'hot',
        ]);

        EventMediaAsset::create([
            'event_id' => $event->id,
            'media_storage_target_id' => $target->id,
            'photo_id' => null,
            'variant' => 'thumbnail',
            'disk' => 'local-ssd',
            'path' => 'events/'.$event->id.'/thumb.jpg',
            'size_bytes' => 512,
            'status' => 'failed',
        ]);

        $health = Mockery::mock(StorageHealthService::class);
        $health->shouldReceive('getCapacity')->andReturn([
            'status' => 'ok',
            'total' => 100,
            'free' => 10,
            'used' => 90,
            'percentage' => 90,
            'path' => storage_path('app'),
        ]);
        $this->app->instance(StorageHealthService::class, $health);

        $this->artisan('storage:monitor')
            ->expectsOutput('Storage monitor finished: 1 targets, 2 alerts.')
            ->assertExitCode(0);

        $snapshot = Cache::get('storage:monitor:last');

        $this->assertNotNull($snapshot);
        $this->assertCount(1, $snapshot['targets']);
        $this->assertSame('local-ssd', $snapshot['targets'][0]['key']);
        $this->assertSame(2, $snapshot['targets'][0]['assets']['total']);
        $this->assertGreaterThanOrEqual(1, count($snapshot['alerts']));
    }
}
