<?php

namespace App\Models;

use App\Entities\HPILOEntity;

class HPILOModel extends BaseModel
{
	protected $table = "tw_hpilo";
	protected $returnType = HPILOEntity::class;
	public function __construct()
	{
		parent::__construct();
		$this->allowedFields = [...$this->allowedFields, ...$this->getFields(), 'model', 'processor', 'memory', 'health', 'power', 'detail',];
		$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
	}
	public function getTitle(): string
	{
		return 'customer';
	}
	public function getFields(string $action = ""): array
	{
		$fields = [$this->getTitle(), 'ip', 'port', 'id', 'passwd', 'status'];
		switch ($action) {
			case "index":
			case "excel":
				return [$this->getTitle(), 'ip', 'port', 'model', 'processor', 'memory', 'health', 'power', 'status', 'created_at'];
				break;
			case "view":
				return [$this->getTitle(), 'ip', 'port', 'id', 'model', 'processor', 'memory', 'health', 'power', 'detail', 'status', 'updated_at', 'created_at'];
				break;
			default:
				return $fields;
				break;
		}
	}
	public function getFieldFilters(array $fields = array()): array
	{
		return ["power", "status", ...$fields];
	}
	protected function getFieldRule(string $field, array $rules, string $action = ""): array
	{
		switch ($field) {
			case "customer":
				$rules[$field] =  "required|trim|string";
				$rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
				break;
			case "ip":
				$rules[$field] = "required|valid_ip[ipv4]";
				break;
			case "port":
				$rules[$field] = "required|numeric";
				break;
			case "id":
				$rules[$field] = "required|string";
				break;
			case "passwd":
				$rules[$field] = "required|trim|string";
				$rules["confirmpassword"] = "required|trim|string|matches[passwd]";
				break;
			case "model":
			case "processor":
			case "memory":
			case "health":
			case "power":
			case "detail":
				$rules[$field] = "required|string";
				break;
			default:
				$rules = parent::getFieldRule($field, $rules, $action);
				break;
		}
		return $rules;
	}
	public function getEntity($conditions): HPILOEntity
	{
		return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
	}
	protected function changeFormData(string $action, string $field, array $formDatas, $entity)
	{
		switch ($field) {
			case "passwd": //API에서 사용하므로 암호를 바꾸면 않됨
				if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
					$entity->$field = $formDatas[$field];
				}
				break;
			default:
				return parent::changeFormData($action, $field, $formDatas, $entity);
				break;
		}
		return $entity;
	}
	public function create(array $formDatas): HPILOEntity
	{
		return $this->create_process(new HPILOEntity(), $formDatas);
	}
	public function modify(HPILOEntity $entity, array $formDatas): HPILOEntity
	{
		return $this->modify_process($entity, $formDatas);
	}

	//Index관련
	public function setIndexWordFilter(string $word)
	{
		parent::setIndexWordFilter($word);
		$this->orLike($this->getTitle(), $word, "both"); //befor , after , both
	}
}
