<?php

namespace SHServ\Helpers;

use \SHServ\Models\MetaManager;
use \SHServ\Entities\Meta;

class MetaWrap {
	protected static $meta_manager_instance;
	protected $assignment;
	protected $ent_id;

	public function __construct(String $table_name, int $ent_id) {
		$this -> assignment = $table_name;
		$this -> ent_id = $ent_id;

		if(!self::$meta_manager_instance) {
			self::$meta_manager_instance = new MetaManager();
		}
	}

	public function remove_all(): bool {
		return self::$meta_manager_instance -> remove_all_by_entity(
			$this -> assignment, 
			$this -> ent_id
		);
	}

	public function update(String $name, $value): bool {
		return self::$meta_manager_instance -> create_or_update(
			$name, 
			"{$value}", 
			$this -> assignment, 
			$this -> ent_id
		);
	}

	public function get(String $name): ?Meta {
		return self::$meta_manager_instance -> get_one_by_name(
			$name, 
			$this -> assignment, 
			$this -> ent_id
		);
	}

	public function all(): Array {
		return self::$meta_manager_instance -> get_all_by_entity(
			$this -> assignment, 
			$this -> ent_id
		);
	}

	public function get_value(String $name): String | null {
		$item = $this -> get($name);
		return $item ? $item -> value : null;
	}
}