<?php

namespace App\Resources;

use App\Enumerations\EatingHabit;
use App\Enumerations\EfzStatus;
use App\Enumerations\ParticipationType;
use App\Models\EventParticipant;
use App\ValueObjects\Age;
use Illuminate\Http\Resources\Json\JsonResource;

class EventParticipantResource extends JsonResource
{
    function __construct(EventParticipant $participant)
    {
        parent::__construct($participant);
    }

    public function toArray($request) : array
    {
        $event = $this->resource->event;

        $amountLeft = clone $this->resource->amount;
        if ($this->resource->amount_paid !== null) {
            $amountLeft->subtractAmount($this->resource->amount_paid);
        }

        $presenceDays = $this->resource->arrival_date->diff($this->resource->departure_date)->days;
        $presenceDaysSupport = $presenceDays;

        if ($presenceDaysSupport === 0) {
            $presenceDaysSupport = 1;
        } else {
            $presenceDaysSupport = $presenceDaysSupport - 1;
        }




        return array_merge(
            $this->resource->toArray(),
            [
                'birthdayDate' => $this->resource->birthday->format('Y-m-d'),
                'arrivalDate' => $this->resource->arrival_date->format('Y-m-d'),
                'departureDate' => $this->resource->departure_date->format('Y-m-d'),
                'registerDate' => $this->resource->created_at->format('d.m.Y'),
                'unregistered' => $this->resource->unregistered_at !== null,
                'unregisteredAt' => $this->resource->unregistered_at?->format('d.m.Y'),
                'fullname' => $this->resource->getFullName(),
                'age' => new Age($this->resource->birthday)->getAge(),
                'localgroup' => $this->resource->localGroup()->first()->name,
                'swimmingPermission' => $this->resource->swimmingPermission()->first()->short,
                'extendedFirstAid' => $this->resource->firstAidPermission()->first()->name,
                'tetanusVaccination' => $this->resource->tetanus_vaccination?->format('d.m.Y') ?? 'Unbekannt',
                'presenceDays' => ['real' => $presenceDays, 'support' => $presenceDaysSupport],
                'participationType' => ParticipationType::where(['slug' => $this->resource->participation_type])->first()->name,
                'needs_payment' => $this->resource->amount->getAmount() > 0 && $event->pay_direct && $this->resource->amount_paid?->getAmount() < $this->resource->amount->getAmount(),
                'nicename' => $this->resource->getNicename(),
                'arrival' => $this->resource->arrival_date->format('d.m.Y'),
                'departure' => $this->resource->departure_date->format('d.m.Y'),
                'amount_left_string' => $amountLeft->toString(),
                'amount_left_value' => $amountLeft->getAmount(),
                'email_1' => $this->resource->email_1,
                'amountPaid' => ['value' => $this->resource->amount_paid, 'readable' => $this->resource->amount_paid?->toString() ?? '0,00 Euro', 'short' => $this->resource->amount_paid?->getFormattedAmount() ?? '0,00'],
                'amountExpected' => ['value' => $this->resource->amount, 'readable' => $this->resource->amount?->toString() ?? '0,00 Euro', 'short' => $this->resource->amount?->getFormattedAmount() ?? '0,00'],
                'alcoholicsAllowed' => new Age($this->resource->birthday)->getAge() >= $event->alcoholics_age,
                'localGroupPostcode' => $this->resource->localGroup()->first()->postcode,
                'localGroupCity' => $this->resource->localGroup()->first()->city,
                'state' => config('postCode.map.' . $this->resource->postcode),
                'localGroupState' => config('postCode.map.' . $this->resource->localGroup()->first()->postcode),
                'birthday' => $this->resource->birthday->format('d.m.Y'),
                'eatingHabit' => EatingHabit::where('slug', $this->resource->eating_habit)->first()->name,
                'cocColor' => match ($this->resource->efz_status) {
                    EfzStatus::EFZ_STATUS_CHECKED_VALID => 'bg-green',
                    EfzStatus::EFZ_STATUS_NOT_REQUIRED => 'bg-green',
                    EfzStatus::EFZ_STATUS_NOT_CHECKED => 'bg-yellow',
                    EfzStatus::EFZ_STATUS_CHECKED_INVALID => 'bg-red',
                },
                'efzStatusReadable' => match($this->resource->efz_status) {
                    EfzStatus::EFZ_STATUS_CHECKED_VALID => 'Gültig',
                    EfzStatus::EFZ_STATUS_CHECKED_INVALID => 'Nicht eingereicht',
                    EfzStatus::EFZ_STATUS_NOT_CHECKED => 'Nicht geprüft',
                    EfzStatus::EFZ_STATUS_NOT_REQUIRED => 'Nicht erforderlich',
                },
                'eventName' => $this->resource->event()->first()->name,
                'arrivalDateReadable' => $this->resource->arrival_date->format('d.m.Y'),
                'departureDateReadable' => $this->resource->departure_date->format('d.m.Y'),
            ]
        );
    }
}
