<?php

namespace App\Repositories;

use App\Domains\Dashboard\Actions\UpdatePersonalData\UpdatePersonalDataRequest;
use App\Models\User;
use DateTime;

class UserRepository {
    public function findByUsername(string $username) : ?User {
        return User::where(['username' => $username])->first();
    }

    public function checkVerificationToken(User $user, string $token) : bool {
        if (new DateTime() > DateTime::createFromFormat('Y-m-d H:i:s', $user->activation_token_expires_at)) {
            return false;
        }

        return $token === $user->activation_token;
    }

    public function getCurrentUserDetails() : array {
        $user = auth()->user();

        $return = [
            'userId' => null,
            'userName' => '',
            'userEmail' => '',
            'userTelephone' => '',
            'userAccountOwner' => '',
            'userAccountIban' => '',
        ];

        if (null !== auth()->user()) {
            $return = [
                'userId' => $user->id,
                'userName' => trim($user->getOfficialName()),
                'userEmail' => trim($user->email),
                'userTelephone' => trim($user->phone),
                'userAccountOwner' => trim($user->bank_account_owner),
                'userAccountIban' => trim($user->bank_account_iban),
            ];

            if ($return['userAccountOwner'] === '') {
                $return['userAccountOwner'] = $return['userName'];
            }
        }

        return $return;
    }

    public function getPersonalData(User $user): array
    {
        return [
            'firstname'          => $user->firstname ?? '',
            'lastname'           => $user->lastname ?? '',
            'birthday'           => $user->birthday ?? null,
            'nickname'           => $user->nickname ?? null,
            'email'              => $user->email ?? null,
            'phone'              => $user->phone ?? null,
            'address_1'          => $user->address_1 ?? null,
            'address_2'          => $user->address_2 ?? null,
            'postcode'           => $user->postcode ?? null,
            'city'               => $user->city ?? null,
            'medications'        => $user->medications ?? null,
            'allergies'          => $user->allergies ?? null,
            'intolerances'       => $user->intolerances ?? null,
            'tetanus_vaccination' => $user->tetanus_vaccination ?? null,
            'eating_habits'      => $user->eating_habits ?? null,
            'swimming_permission' => $user->swimming_permission ?? null,
            'first_aid_permission' => $user->first_aid_permission ?? null,
            'bank_account_owner' => $user->bank_account_owner ?? null,
            'bank_account_iban'  => $user->bank_account_iban ?? null,
        ];
    }

    public function updatePersonalData(UpdatePersonalDataRequest $request): void
    {
        $request->user->update([
            'nickname'            => $request->nickname,
            'email'               => $request->email,
            'phone'               => $request->phone,
            'address_1'           => $request->address1,
            'address_2'           => $request->address2,
            'postcode'            => $request->postcode,
            'city'                => $request->city,
            'medications'         => $request->medications,
            'allergies'           => $request->allergies,
            'tetanus_vaccination' => $request->tetanusVaccination,
            'intolerances'        => $request->intolerances,
            'eating_habits'       => $request->eatingHabits,
            'swimming_permission' => $request->swimmingPermission,
            'first_aid_permission' => $request->firstAidPermission,
            'bank_account_owner'  => $request->bankAccountOwner,
            'bank_account_iban'   => $request->bankAccountIban,
            'birthday'            => $request->birthday,
        ]);
    }
}
