<?php

namespace Tests\Feature\Api\Tenant;

use App\Enums\DataExportScope;
use App\Jobs\GenerateDataExport;
use App\Models\DataExport;
use App\Models\Event;
use App\Models\Tenant;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Tests\Feature\Tenant\TenantTestCase;

class DataExportApiTest extends TenantTestCase
{
    public function test_tenant_can_request_event_export(): void
    {
        Queue::fake();

        $event = Event::factory()->create(['tenant_id' => $this->tenant->id]);

        $response = $this->authenticatedRequest('POST', '/api/v1/tenant/exports', [
            'scope' => 'event',
            'event_id' => $event->id,
            'include_media' => true,
        ]);

        $response->assertStatus(202)
            ->assertJsonPath('data.scope', 'event')
            ->assertJsonPath('data.status', DataExport::STATUS_PENDING);

        $this->assertDatabaseHas('data_exports', [
            'tenant_id' => $this->tenant->id,
            'event_id' => $event->id,
            'scope' => DataExportScope::EVENT->value,
            'include_media' => true,
        ]);

        Queue::assertPushed(GenerateDataExport::class);
    }

    public function test_exports_index_filters_to_tenant_scopes(): void
    {
        $event = Event::factory()->create(['tenant_id' => $this->tenant->id]);
        $otherTenant = Tenant::factory()->create();
        $otherEvent = Event::factory()->create(['tenant_id' => $otherTenant->id]);

        DataExport::query()->create([
            'user_id' => $this->tenantUser->id,
            'tenant_id' => $this->tenant->id,
            'event_id' => $event->id,
            'scope' => DataExportScope::EVENT->value,
            'status' => DataExport::STATUS_READY,
            'include_media' => false,
        ]);

        DataExport::query()->create([
            'user_id' => $this->tenantUser->id,
            'tenant_id' => $this->tenant->id,
            'scope' => DataExportScope::USER->value,
            'status' => DataExport::STATUS_READY,
            'include_media' => false,
        ]);

        DataExport::query()->create([
            'user_id' => $this->tenantUser->id,
            'tenant_id' => $otherTenant->id,
            'event_id' => $otherEvent->id,
            'scope' => DataExportScope::EVENT->value,
            'status' => DataExport::STATUS_READY,
            'include_media' => true,
        ]);

        $response = $this->authenticatedRequest('GET', '/api/v1/tenant/exports');

        $response->assertOk();
        $this->assertCount(1, $response->json('data'));
        $this->assertSame($event->id, $response->json('data.0.event.id'));
    }

    public function test_event_export_rejects_foreign_event(): void
    {
        $otherTenant = Tenant::factory()->create();
        $otherEvent = Event::factory()->create(['tenant_id' => $otherTenant->id]);

        $response = $this->authenticatedRequest('POST', '/api/v1/tenant/exports', [
            'scope' => 'event',
            'event_id' => $otherEvent->id,
        ]);

        $response->assertStatus(404);
    }

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

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

        $export = DataExport::query()->create([
            'user_id' => $this->tenantUser->id,
            'tenant_id' => $this->tenant->id,
            'scope' => DataExportScope::TENANT->value,
            'status' => DataExport::STATUS_READY,
            'include_media' => false,
            'path' => 'exports/tenant-export.zip',
            'size_bytes' => 123,
            'expires_at' => now()->addDay(),
        ]);

        $response = $this->authenticatedRequest('GET', route('api.v1.tenant.exports.download', $export));

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