<?php
namespace src\models;
use \core\Model;

class Interaction extends Model {

	public static function save($data) {
			Interaction::insert($data)
		->execute();
	}

	public static function saveClientProspection($data) {
		$ref = Interaction::insert($data)
			->table('prospection')
		->execute();

		return $ref;
	}

	public static function createConversation($data) {
		$ref = Interaction::insert($data)
			->table('crm')
		->execute();

		return $ref;
	}

	public static function updateSalesman($old, $new, $client, $type) {
		Salesman::update(['id_salesman' => $new])
			->table('crm')
			->where('id_salesman', $old)
			->where('id_client', $client)
			->where('type', $type)
		->execute();
	}

	public static function getConversation($ref, $value, $filter) {
		$data = Interaction::select()
			->table('crm')
			->where($ref, $value)
			->where($filter['collum'], $filter['simbol'], $filter['value'])
		->one();

		return $data;
	}

	public static function getConversations($filter, $salesman) {
		$data = Interaction::select('id, id_salesman')
			->table('crm')
			->where($filter['f_interaction_category'], $filter['f_interaction_category_simbol'], $filter['f_interaction_category_value'])
			->where($filter['f_interactiontype'], $filter['f_interactiontype_simbol'], $filter['f_interactiontype_value'])
			->where('id_salesman', $salesman)
		->get();

		return $data;
	}

	public static function getResult($filter, $conversatiton) {
		$data = Interaction::select()
			->where($filter['f_interaction'], $filter['f_interaction_simbol'], $filter['f_interaction_value']) // texto
			->where('id_crm', $conversatiton)
			->orderBy(['id' => 'desc'])
		->one();

		return $data;
	}

	// Administrativo

	public static function getConversationsAdmin($filter) {
		$data = Interaction::select('id, id_salesman')
			->table('crm')
			->where($filter['f_interaction_category'], $filter['f_interaction_category_simbol'], $filter['f_interaction_category_value'])
			->where($filter['f_interactiontype'], $filter['f_interactiontype_simbol'], $filter['f_interactiontype_value'])
			->where($filter['f_interaction_salesman'], $filter['f_interaction_salesman_simbol'], $filter['f_interaction_salesman_value'])
		->get();

		return $data;
	}
	// Fim Administrativo

	public static function verifyExistsNewInteraction($client, $ref, $date) {
		$data = Interaction::select()
			->where('id_client', $client)
			->where('id', '!=', $ref)
			->where('next_interation', '>', $date)
		->count();

		if($data > 0) {
			return true;
		}

		return false;
	}

	public static function getClientsProspection() {
		$data = Interaction::select()
			->table('prospection')
			->orderBy('name', 'ASC')
		->get();

		return $data;
	}


	public static function getClientBySalesman($user) {
		$data = Interaction::select('id_client')
			->where('id_salesman', $user)
			->groupBy('id_client')
		->get();

		return $data;
	}

	public static function getAllClient() {
		$data = Interaction::select('id_client')
			->groupBy('id_client')
		->get();

		return $data;
	}

	public static function getClientProspection($id) {
		$data = Interaction::select()
			->table('prospection')
			->where('id', $id)
		->one();

		return $data;
	}

	public static function getInteractionByClient($client) {
		$data = Interaction::select()
			->where('id_client', $client)
		->one();

		return $data;
	}

	public static function getLastInteraction($conversation, $limit) {
		$data = Interaction::select()
			->where('id_crm', $conversation)
			->orderBy(['date_created' => 'desc'])
			->limit($limit)
		->get();

		return $data;
	}

}