<?php

namespace App\Domains\Event\Actions\SetParticipationFees;

use App\RelationModels\EventParticipationFee;

class SetParticipationFeesCommand {
    private SetParticipationFeesRequest $request;
    public function __construct(SetParticipationFeesRequest $request) {
        $this->request = $request;
    }

    public function execute() : SetParticipationFeesResponse {
        $response = new SetParticipationFeesResponse();
        $this->request->event->sibling_reduction = $this->request->siblingReduction;
        $this->request->event->save();

        $this->cleanBefore();

        $this->request->event->participationFee1()->associate(EventParticipationFee::create([
            'tenant' => app('tenant')->slug,
            'type' => $this->request->participationFeeFirst['type'],
            'name' => $this->request->participationFeeFirst['name'],
            'description' => $this->request->participationFeeFirst['description'],
            'amount_standard' => $this->request->participationFeeFirst['amount_standard']->getAmount(),
            'amount_reduced' => null !== $this->request->participationFeeFirst['amount_reduced'] ? $this->request->participationFeeFirst['amount_reduced']->getAmount() : null,
            'amount_solidarity' => null !== $this->request->participationFeeFirst['amount_solidarity'] ? $this->request->participationFeeFirst['amount_solidarity']->getAmount() : null,
        ]))->save();

        if ($this->request->participationFeeSecond !== null) {
            $this->request->event->participationFee2()->associate(EventParticipationFee::create([
                'tenant' => app('tenant')->slug,
                'type' => $this->request->participationFeeSecond['type'],
                'name' => $this->request->participationFeeSecond['name'],
                'description' => $this->request->participationFeeSecond['description'],
                'amount_standard' => $this->request->participationFeeSecond['amount_standard']->getAmount(),
                'amount_reduced' => null !== $this->request->participationFeeSecond['amount_reduced'] ? $this->request->participationFeeSecond['amount_reduced']->getAmount() : null,
                'amount_solidarity' => null !== $this->request->participationFeeSecond['amount_solidarity'] ? $this->request->participationFeeSecond['amount_solidarity']->getAmount() : null,
            ]))->save();
        }

        if ($this->request->participationFeeThird !== null) {
            $this->request->event->participationFee3()->associate(EventParticipationFee::create([
                'tenant' => app('tenant')->slug,
                'type' => $this->request->participationFeeThird['type'],
                'name' => $this->request->participationFeeThird['name'],
                'description' => $this->request->participationFeeThird['description'],
                'amount_standard' => $this->request->participationFeeThird['amount_standard']->getAmount(),
                'amount_reduced' => null !== $this->request->participationFeeThird['amount_reduced'] ? $this->request->participationFeeThird['amount_reduced']->getAmount() : null,
                'amount_solidarity' => null !== $this->request->participationFeeThird['amount_solidarity'] ? $this->request->participationFeeThird['amount_solidarity']->getAmount() : null,
            ]))->save();
        }

        if ($this->request->participationFeeFourth !== null) {
            $this->request->event->participationFee4()->associate(EventParticipationFee::create([
                'tenant' => app('tenant')->slug,
                'type' => $this->request->participationFeeFourth['type'],
                'name' => $this->request->participationFeeFourth['name'],
                'description' => $this->request->participationFeeFourth['description'],
                'amount_standard' => $this->request->participationFeeFourth['amount_standard']->getAmount(),
                'amount_reduced' => null !== $this->request->participationFeeFourth['amount_reduced'] ? $this->request->participationFeeFourth['amount_reduced']->getAmount() : null,
                'amount_solidarity' => null !== $this->request->participationFeeFourth['amount_solidarity'] ? $this->request->participationFeeFourth['amount_solidarity']->getAmount() : null,
            ]))->save();
        }

        $this->request->event->save();
        $response->success = true;

        return $response;
    }

    private function cleanBefore() {
        if ($this->request->event->participationFee1()->first() !== null) {
            $this->request->event->participationFee1()->first()->delete();
        }

        if ($this->request->event->participationFee2()->first() !== null) {
            $this->request->event->participationFee2()->first()->delete();
        }

        if ($this->request->event->participationFee3()->first() !== null) {
            $this->request->event->participationFee3()->first()->delete();
        }

        if ($this->request->event->participationFee4()->first() !== null) {
            $this->request->event->participationFee4()->first()->delete();
        }

        $this->request->event->save();
    }


}
