<?php

namespace Tests\Feature;

use App\Jobs\GenerateDataExport;
use App\Models\DataExport;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class ProfileDataExportTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_request_data_export(): void
    {
        Queue::fake();

        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);

        $response = $this->actingAs($user)->post('/profile/data-exports');

        $response->assertRedirect();
        $this->assertDatabaseCount('data_exports', 1);
        Queue::assertPushed(GenerateDataExport::class);
    }

    public function test_ready_export_can_be_downloaded(): void
    {
        Storage::fake('local');

        $tenant = Tenant::factory()->create();
        $user = User::factory()->create(['tenant_id' => $tenant->id]);

        Storage::disk('local')->put('exports/demo.zip', 'demo-content');

        $export = DataExport::create([
            'user_id' => $user->id,
            'tenant_id' => $tenant->id,
            'status' => DataExport::STATUS_READY,
            'path' => 'exports/demo.zip',
            'size_bytes' => 123,
            'expires_at' => now()->addDay(),
        ]);

        $response = $this->actingAs($user)->get(route('profile.data-exports.download', $export));

        $response->assertOk();
        $response->assertHeader('content-disposition');
    }
}
