<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Group;
use App\Models\GroupAsset;
use App\Services\GroupAssetService;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;

class GroupAssetController extends Controller
{
    public function __construct(private readonly GroupAssetService $assets)
    {
    }

    public function download(Request $request, Group $group, GroupAsset $asset): StreamedResponse
    {
        $this->authorize('view', $group);
        abort_unless((int) $asset->group_id === (int) $group->id, 404);
        abort_unless($asset->canBeViewedBy($request->user()), 403);

        return $this->assets->downloadResponse($asset);
    }
}