<?php

namespace App\Services;

use Illuminate\Support\Facades\Storage;
use Throwable;

class GameClientDownloadService
{
    public function filename(): string
    {
        return basename($this->objectKey());
    }

    public function temporaryUrl(): string
    {
        $disk = Storage::disk(config('moonwell.client.disk'));
        $objectKey = $this->objectKey();
        $filename = $this->filename();

        try {
            return $disk->temporaryUrl(
                $objectKey,
                now()->addMinutes(config('moonwell.client.temporary_url_minutes')),
                ['ResponseContentDisposition' => sprintf('attachment; filename="%s"', $filename)],
            );
        } catch (Throwable) {
            return $disk->url($objectKey);
        }
    }

    private function objectKey(): string
    {
        return (string) config('moonwell.client.object_key');
    }
}
