<?php

namespace App\Domains\Event\Actions\SetParticipationState;

use App\Mail\ParticipantParticipationMails\EventSignUpSuccessfullMail;
use App\Mail\ParticipantParticipationMails\ParticipantSignOffMail;
use Illuminate\Support\Facades\Mail;

class SetParticipationStateCommand {
    function __construct(private SetParticipationStateSignoffRequest|SetParticipationStateReSignonRequest $request) {}

    public function execute() : SetParticipationStateResponse {
        $response = new SetParticipationStateResponse();


        switch (true) {
            case $this->request instanceof SetParticipationStateSignoffRequest:
                $this->request->participant->unregistered_at = $this->request->date;
                $this->request->participant->save();
                $response->success = true;
                Mail::to($this->request->participant->email_1)->send(new ParticipantSignOffMail(
                    participant: $this->request->participant,
                ));

                if ($this->request->participant->email_2 !== null) {
                    Mail::to($this->request->participant->email_2)->send(new ParticipantSignOffMail(
                        participant: $this->request->participant,
                    ));
                }

                break;
            case $this->request instanceof SetParticipationStateReSignonRequest:
                $this->request->participant->unregistered_at = null;
                $this->request->participant->save();

                Mail::to($this->request->participant->email_1)->send(new EventSignUpSuccessfullMail(
                    participant: $this->request->participant,
                ));

                if ($this->request->participant->email_2 !== null) {
                    Mail::to($this->request->participant->email_2)->send(new EventSignUpSuccessfullMail(
                        participant: $this->request->participant,
                    ));
                }

                break;
        }



        return $response;
    }
}
