<?php

defined('BASEPATH') or exit('No direct script access allowed');

class User_model extends MY_Model
{

	public function one($id)
	{
		return $this->db->from('user u')
			->join('user_details ud', 'u.id = ud.user_id')
			->where('u.id', $id)
			->select('u.id, ud.full_name, ud.mobile, ud.email')
			->get()->row_array();
	}

	public function email_search($email)
	{
		return $this->db->from('user u')
			->join('user_details ud', 'u.id = ud.user_id')
			->where('ud.email', $email)
			->select('u.id, ud.full_name, ud.mobile, ud.email')
			->get()->row_array();
	}

	public function create($params)
	{
		$username = $this->random_string(16);
		$data = [
			'user' => $username,
			'pass' => sha1($username),
		];
		$this->db->set('created_at', 'NOW()', false);
		$this->db->set('updated_at', 'NOW()', false);
		$this->db->insert('user', $data);
		$id = $this->db->insert_id();
		$data = [
			'user_id' => $id,
			'full_name' => $params['participant'],
			'mobile' => '',
			'email' => $params['email'],
		];
		$this->db->set('created_at', 'NOW()', false);
		$this->db->set('updated_at', 'NOW()', false);
		$this->db->insert('user_details', $data);
		return $this->one($id);
	}
}
