<?php

namespace app\models;

use app\models\Users;
use \MongoDate;
use lithium\util\Validator;

Class entry extends \lithium\data\Model {
	public static function __init() {
		parent::__init();

	}

	public $_schema = array('_id' => array('type' => 'id'));
	public $validates = array(
		    'my_watched_episodes' => array('numeric', 'message' => 'please enter a number'),
			'my_start_date' => array('date', 'message' => 'Please enter a valid date'),
			'my_finish_date' => array('date', 'message' => 'Please enter a valid date'),
			'my_score' => array(array('inRange' => array('min' => 0, 'max' => '10'), 'message' => 'Enter a valid score'),
								array('numeric', 'message' => 'Please enter a number')),
			//'my_status' => array('isValidStatus', 'message' => 'please enter valid status'),
			'my_times_watched' => array('numeric', 'message' => 'This must be a number'),
		);

	//Add timestamping to entries. :TODO:

	



	public function add($entity, $username)
	{
		$updateData = array('$push' => array('animelist' => $entity->data()));
		$conditions = compact('username');
		$result = Entry::update($updateData, $conditions, array('atomic' => false));
		return $result; 
	}

	/*
	//Got lazy, the proper way to do it is above, but needs a bit of fiddling. 
	//The below code works, but forces mongo to resave the entire record, which takes
	//longer than just updating what has chaged. 
	public function add($entity, $username)
	{
		$user = User::find('first', array('conditions' => compact('username')));
		$entity->created_on = new MongoDate();
		$entity->updated_on = new MongoDate();

		$user->animelist[] = $entity;

		//if (Validator::check($entity->data(), $this->validates, array('skipEmpty' => 'true'))) {
			$return = $user->save(null, array('validate' => false));
			var_dump($entity);
			exit();
		/*
		else 
		{
			return false;
		}
		
	}
	*/

	public function edit($entity, $username)
	{
		$user = User::find('first', array('conditions' => compact('username')));
		$entity->created_on = new MongoDate();
		$entity->updated_on = new MongoDate();
		$user->animelist[] = $entity;
		return $user->save(null, array('validate' => false));
	}
}