<?php

namespace Entity;

class AriaChat extends \App\Entity\GeneralAbstract
{
    /**
     * Indicate if operations for the current entity must be done inside a transaction or not.
     * @var bool
     */
    protected $saveInsideTransaction = true;

    /**
     * Set the DB table name for this entity
     */
    protected function setTableName(): void
    {
        global $DBTAriaChats;
        $this->tableName = $DBTAriaChats;
    }

	protected function getAttribute($var)
	{
		// CHECK EVERY POSSIBLE VALUES THAT $var MAY HAVE -----------------------------------------
		switch ($var) {

			// all messages list, related to this chat
			case 'messagesList': {
				$chatsListObj = new \App\EntityList\AriaChats();
				$chatsListObj->setFilter('ariaChatMessages.idChat', '=', $this->id);
				$chatsListObj->setColumns(['ariaChatMessages.*']);
				$chatsListObj->setOrderBy('ariaChatMessages.id', 'ASC');
				$this->fields[$var] = $chatsListObj->exportRecords();
				return $this->fields[$var];
			}

			// user
			case 'user': {
				return \Entity\User::select($this->fields['idUser'], $this->selectForUpdate);
			}

			// addUser
			case 'addUser': {
				return \Entity\User::select($this->fields['idAddUser'], $this->selectForUpdate);
			}

			// updateUser
			case 'updateUser': {
				return \Entity\User::select($this->fields['idUpdateUser'], $this->selectForUpdate);
			}

			// the rest of the fields (most of them are just copied)
			default: {
				return parent::getAttribute($var);
			}
		}
	}

	protected function setAttribute($var, $value)
	{
		// check that operation is okay
		if ($this->operation!='insert' and $this->operation!='update') {
			throw new \Exception("You cannot use __set magic method on `{$this->operation}` operation in `".__CLASS__."` class.");
		}

		switch ($var) {

			// simple fields
			case 'summary': case 'idUser': case 'reportSource':
				$this->fields[$var] = $value;
			break;

			// the field is not valid
			default: {
				throw new \Exception("The field '$var' is not valid for the `".__METHOD__."` entity call.");
			}
		}

	}

}
