<?php

namespace App\Domains\Event\Controllers;

use App\Domains\Event\Actions\SetParticipationState\SetParticipationStateCommand;
use App\Domains\Event\Actions\SetParticipationState\SetParticipationStateSignoffRequest;
use App\Scopes\CommonController;
use DateTime;
use Illuminate\Http\Request;

class ParticipantSignOffController extends CommonController
{
    public function __invoke(string $participantIdentifier, Request $request) {
        $participant = $this->eventParticipants->getByIdentifier($participantIdentifier, $this->events);

        $signOffDate = DateTime::createFromFormat('Y-m-d', $request->input('cancel_date'));

        $signOffRequest = new SetParticipationStateSignoffRequest($participant, $signOffDate);
        $signOffCommand = new SetParticipationStateCommand($signOffRequest);
        $signOffCommand->execute();

        return response()->json([
            'status' => 'success',
            'identifier' => $participant->identifier,
            'message' => 'Die Abmeldung wurde erfolgreich durchgeführt.'
        ]);

    }
}
