<?php

namespace SHServ\Factory;

use \SHServ\Entities\Profile;
use \SHServ\Entities\User;
use \SHServ\Entities\Meta;

class Creator {
	public function create_user(String $alias, String $email, String $password) {
		$password_hash = sha1($password);

		$uid = app() -> thin_builder -> insert(User::$table_name, [
			"alias" => $alias,
			"email" => $email,
			"password" => $password_hash,
			"create_at" => date("Y-m-d H:i:s")
		]);

		return $uid ? new User($uid) : null;
	}

	public function create_profile(Int $uid) {
		$profile_id = app() -> thin_builder -> insert(Profile::$table_name, [
			"uid" => $uid,
			"create_at" => date("Y-m-d H:i:s")
		]);

		return $profile_id ? new Profile($profile_id) : null;
	}

	public function create_meta(Int $ent_id, String $assignment, String $name, $value): Meta {
		$data = [
			"ent_id" => $ent_id,
			"assignment" => $assignment,
			"name" => $name,
			"value" => $value,
			"create_at" => date("Y-m-d H:i:s")
		];

		$meta_id = app() -> thin_builder -> insert(Meta::$table_name, $data);

		return new Meta($meta_id, array_merge(
			[
				"id" => $meta_id, 
				"update_at" => $data["create_at"]
			],
			$data
		));
	}
}