<?php

namespace App\Domains\Admin\Actions\UpdateUser;

class UpdateUserAction
{
    public function __construct(private UpdateUserRequest $request)
    {
    }

    public function execute(): UpdateUserResponse
    {
        $response = new UpdateUserResponse();

        $allowedFields = [
            'firstname', 'lastname', 'nickname', 'email', 'phone', 'birthday',
            'membership_id', 'address_1', 'address_2', 'postcode', 'city',
            'eating_habits', 'swimming_permission', 'first_aid_permission',
            'bank_account_owner', 'bank_account_iban',
            'medications', 'allergies', 'intolerances',
            'user_role_local_group',
        ];

        if ($this->request->isLvTenant) {
            $allowedFields[] = 'local_group';
            if (!$this->request->isOwnUser) {
                $allowedFields[] = 'user_role_main';
            }
        }

        $data = array_intersect_key($this->request->data, array_flip($allowedFields));
        $this->request->user->update($data);

        $response->success = true;
        $response->message = 'Benutzerdaten wurden gespeichert.';

        return $response;
    }
}
