<?php

namespace App\Domains\Event\Controllers;

use App\Domains\CostUnit\Actions\CreateCostUnit\CreateCostUnitCommand;
use App\Domains\CostUnit\Actions\CreateCostUnit\CreateCostUnitRequest;
use App\Domains\Event\Actions\CreateEvent\CreateEventCommand;
use App\Domains\Event\Actions\CreateEvent\CreateEventRequest;
use App\Domains\Event\Actions\SetCostUnit\SetCostUnitCommand;
use App\Domains\Event\Actions\SetCostUnit\SetCostUnitRequest;
use App\Enumerations\CostUnitType;
use App\Enumerations\ParticipationFeeType;
use App\Providers\InertiaProvider;
use App\Resources\EventResource;
use App\Scopes\CommonController;
use App\ValueObjects\Amount;
use DateTime;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateController extends CommonController {
    public function __invoke() {
        return new InertiaProvider('Event/Create', [
            'emailAddress' => auth()->user()->email,
            'eventAccount' => $this->tenant->account_name,
            'eventIban' => $this->tenant->account_iban,
            'eventPayPerDay' => $this->tenant->slug === 'lv' ? true : false,
            'participationFeeType' => $this->tenant->slug === 'lv' ?
                ParticipationFeeType::PARTICIPATION_FEE_TYPE_FIXED :
                ParticipationFeeType::PARTICIPATION_FEE_TYPE_SOLIDARITY,
        ])->render();
    }

    public function doCreate(Request $request) : JsonResponse {

        $eventBegin = DateTime::createFromFormat('Y-m-d', $request->input('eventBegin'));
        $eventEnd = DateTime::createFromFormat('Y-m-d', $request->input('eventEnd'));
        $eventEarlyBirdEnd = DateTime::createFromFormat('Y-m-d', $request->input('eventEarlyBirdEnd'));
        $registrationFinalEnd = DateTime::createFromFormat('Y-m-d', $request->input('eventRegistrationFinalEnd'));
        $participationFeeType = ParticipationFeeType::where('slug', $request->input('eventParticipationFeeType'))->first();
        $payPerDay = $request->input('eventPayPerDay');

        $billingDeadline = clone $eventEnd;
        $billingDeadline->modify('+6 weeks');

        $createRequest = new CreateEventRequest(
            $request->input('eventName'),
            $request->input('eventLocation'),
            $request->input('eventPostalCode'),
            $request->input('eventEmail'),
            $eventBegin,
            $eventEnd,
            $eventEarlyBirdEnd,
            $registrationFinalEnd,
            $request->input('eventEarlyBirdEndAmountIncrease'),
            $participationFeeType,
            $request->input('eventAccount'),
            $request->input('eventIban'),
            $payPerDay
        );

        $wasSuccessful = false;

        $createCommand = new CreateEventCommand($createRequest);
        $result = $createCommand->execute();
        if ($result->success) {
            $createCostUnitRequest = new CreateCostUnitRequest(
                $result->event->name,
                CostUnitType::COST_UNIT_TYPE_EVENT,
                Amount::fromString('0,25'),
                true,
                $billingDeadline
            );

            $createCostUnitCommand = new CreateCostUnitCommand($createCostUnitRequest);
            $costUnitResponse = $createCostUnitCommand->execute();

            if ($costUnitResponse->success) {
                $costUnitUpdateRequest = new SetCostUnitRequest($result->event, $costUnitResponse->costUnit);
                $costUnitUpdateCommand = new SetCostUnitCommand($costUnitUpdateRequest);
                $costUnitSetResponse = $costUnitUpdateCommand->execute();
                $wasSuccessful = $costUnitSetResponse->success;
            }
        }

        if ($wasSuccessful) {
            return response()->json([
                'status' => 'success',
                'event' => new EventResource($costUnitUpdateRequest->event)->toArray($request)
            ]);
        } else {
            return response()->json([
                'status' => 'error',
                'message' => 'Die Veranstaltung konnte nicht angelegt werden.'
            ]);
        }
    }
}

