<?php
include_once dirname(dirname(__DIR__)) . '/bootstrap/autoload.php';

sanitize();

$user = Model::factory(\Incoviba\common\User::class)->where('name', 'guest')->findOne();
if ($user === false) {
  $user = Model::factory(\Incoviba\common\User::class)->create(['name' => 'guest']);
  $user->save();
}
$role = Model::factory(\Incoviba\common\Role::class)->where('description', 'guest')->findOne();
if ($role === false) {
  $role = Model::factory(\Incoviba\common\Role::class)->create(['description' => 'guest']);
  $role->save();
}
$usrRl = Model::factory(\Incoviba\common\UserRole::class)->where('user', $user->id)->where('role', $role->id)->findOne();
if ($usrRl === false) {
  $usrRl = Model::factory(\Incoviba\common\UserRole::class)->create(['user' => $user->id, 'role' => $role->id]);
  $usrRl->save();
}
$locations = Model::factory(\Incoviba\common\Location::class)->where('controller', 'auth')->findMany();
foreach ($locations as $location) {
  $permission = Model::factory(\Incoviba\common\Permission::class)->where('type', 2)->where('ext_id', $role->id)->where('access', 1)->where('location', $location->id)->findOne();
  if ($permission === false) {
    $permission = Model::factory(\Incoviba\common\Permission::class)->create([
      'type' => 2,
      'ext_id' => $role->id,
      'access' => 1,
      'location' => $location->id
    ]);
    $permission->save();
  }
}

header('Location: next_step.php?step=create_guest');
?>
