<?php

Kirby::plugin('kangaroopunch/kangaworld-integration', [

	'api' => [
		'routes' => [
			[
				'pattern' => 'kp/kangaworld/v1',
				'method'  => 'POST',
				'action'  => function() {

					$response = array();
					$response['result'] = 'false';
					$response['reason'] = 'Unknown error.';

					switch (get('command')) {

						case 'USER_CREATE':
							try {
								kirby()->users()->create([
									'name'      => get('name'),
									'email'     => get('email'),
									'password'  => get('password'),
									'language'  => 'en',
									'role'      => 'user'
								]);
								$response['result'] = 'true';
								$response['reason'] = 'User created.';
							} catch(Exception $e) {
								$response['reason'] = $e->getMessage();
							}
							break;

							case 'USER_GET':
								try {
									$user = kirby()->users()->findByKey(get('email'));
									//$response['userraw']  = dump($user);
									$response['name']     = $user->username();
									$response['email']    = $user->email();
									$response['password'] = $user->password();
									$response['language'] = $user->language();
									$response['role']     = $user->role()->id();
									$response['result'] = 'true';
									$response['reason'] = 'User found.';
								} catch(Exception $e) {
									$response['reason'] = $e->getMessage();
								}
								break;

						default:
							$response['reason'] = 'Invalid command.';
							break;
					}

					return $response;
				}

			]
		]
	]

]);
