<?php

namespace App\Http\Controllers\SuperAdmin;

use App\Http\Controllers\Controller;
use App\Models\DataExport;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;

class DataExportController extends Controller
{
    public function download(DataExport $export): StreamedResponse
    {
        if (! $export->isReady() || $export->hasExpired() || ! $export->path) {
            abort(404);
        }

        $disk = 'local';

        if (! Storage::disk($disk)->exists($export->path)) {
            abort(404);
        }

        return Storage::disk($disk)->download(
            $export->path,
            sprintf('fotospiel-data-export-%s.zip', $export->created_at?->format('Ymd') ?? now()->format('Ymd')),
            [
                'Cache-Control' => 'private, no-store',
            ]
        );
    }
}
