<?php

namespace App\Domains\Event\Controllers;

use App\Domains\Event\Actions\GenerateIcal\GenerateIcalCommand;
use App\Domains\Event\Actions\GenerateIcal\GenerateIcalRequest;
use App\Models\EventParticipant;
use App\Scopes\CommonController;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class ParticipantIcalController extends CommonController
{
    public function __invoke(string $participantIdentifier, Request $request): Response
    {
        $participant = $this->eventParticipants->getMyParticipationByIdentifier($participantIdentifier);

        if ($participant === null) {
            abort(403, 'Zugriff verweigert.');
        }

        $icalRequest = new GenerateIcalRequest($participant);
        $icalCommand = new GenerateIcalCommand($icalRequest);
        $icalResponse = $icalCommand->execute();

        return response($icalResponse->icalContent, 200, [
            'Content-Type' => 'text/calendar; charset=utf-8',
            'Content-Disposition' => 'attachment; filename="' . $icalResponse->filename . '"',
        ]);
    }
}
