<?php

namespace App\Domains\Event\Controllers;

use App\Domains\Event\Actions\UpdateParticipant\UpdateParticipantCommand;
use App\Domains\Event\Actions\UpdateParticipant\UpdateParticipantRequest;
use App\Enumerations\EfzStatus;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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

        $updateRequest = new UpdateParticipantRequest(
            participant: $participant,
            firstname: $request->input('firstname'),
            lastname: $request->input('lastname'),
            nickname: $request->input('nickname'),
            address_1: $request->input('address_1'),
            address_2: $request->input('address_2'),
            postcode: $request->input('postcode'),
            city: $request->input('city'),
            localgroup: $request->input('localgroup'),
            birthday: $request->input('birthday'),
            email_1: $request->input('email_1'),
            phone_1: $request->input('phone_1'),
            contact_person: $request->input('contact_person'),
            email_2: $request->input('email_2'),
            phone_2: $request->input('phone_2'),
            arrival: $request->input('arrival'),
            departure: $request->input('departure'),
            participationType: $request->input('participationType'),
            eatingHabit: $request->input('eatingHabit'),
            paymentMethod: $request->input('paymentMethod'),
            allergies: $request->input('allergies'),
            intolerances: $request->input('intolerances'),
            medications: $request->input('medications'),
            extendedFirstAid: $request->input('extendedFirstAid'),
            swimmingPermission: $request->input('swimmingPermission'),
            tetanusVaccination: $request->input('tetanusVaccination'),
            notes: $request->input('notes'),
            amountPaid: $request->input('amountPaid'),
            amountExpected: $request->input('amountExpected'),
            cocStatus: $request->input('cocStatus'),
            participationOptions: $request->has('participationOptions') ? (array)$request->input('participationOptions') : null,
            selectedAddons: $request->has('selectedAddons') ? (array)$request->input('selectedAddons') : null,
        );

        $command = new UpdateParticipantCommand($updateRequest);
        $response = $command->execute();

        $data = [
            'status' => $response->success ? 'success' : 'error',
            'identifier' => $participant->identifier,
            'participant' => $response->participant->toResource()->toArray($request),
        ];

        if ($response->cocStatus !== null) {
            $data['cocChanged'] = true;
            $data['coc']['action'] = in_array($response->cocStatus->slug, [
                EfzStatus::EFZ_STATUS_CHECKED_INVALID,
                EfzStatus::EFZ_STATUS_NOT_CHECKED]) ? 'inline' : 'none';
            $data['coc']['statusText'] = $response->cocStatus->name;
            $data['coc']['class'] = match($response->cocStatus->slug) {
                EfzStatus::EFZ_STATUS_CHECKED_INVALID => 'efz-invalid',
                EfzStatus::EFZ_STATUS_NOT_CHECKED => 'efz-not-checked',
                default => 'efz-valid',
            };
        }

        if ($response->amountPaid !== null) {
            $amountLeft = clone($response->amountExpected);
            $amountLeft->subtractAmount($response->amountPaid);

            $data['amountChanged'] = true;
            $data['amount'] = [
                'paid' => $response->amountPaid->toString(),
                'expected' => $response->amountExpected->toString(),
                'actions' => $amountLeft->getAmount() != 0 ? 'inline' : 'none',
                'class' => $amountLeft->getAmount() != 0 ? 'not-paid' : 'paid',
            ];
        }

        return response()->json($data);
    }
}
