<?php

class ProductController {
	public static function api_get_list($list) {
		$db = DB::getInstance();
		$pids = explode(",", $list);
		$out = new Collection();
		foreach ($pids as $pid) {
			$q = $db->query("select * from product where id=:id", ["id" => $pid]);
			if ($r = $db->nextRecord($q)) {
				$out->push($r);
			}
		}
		return $out;
	}

	public static function api_get_mru() {
		$out = new Collection;
		$mru = Session::get("mru");
		if (!$mru) {
			$mru = [];
		}
		foreach ($mru as $p) {
			$prod = new Product($p);
			if ($prod) {
				$out->push($prod);
			}
		}
		return $out;
	}

	public static function api_add_mru($id) {
		$mru = Session::get("mru");
		if (!$mru) {
			$mru = [];
		}
		array_unshift($mru, $id);
		$mru = array_unique($mru);
		while (count($mru) > 5) {
			array_pop($mru);
		}
		Session::set("mru", $mru);
		return ProductController::api_get_mru();
	}

	public static function api_search() {

		return Product::find([["full_path", "like", "%" . $_POST['search'] . "%"]])->orderBy("full_path")->all();
	}

	public static function api_add_child($_request, $id) {
		$p = new Product;
		$p->parent = $id;
		$p->title = $_request->put("title");
		$p->save();
		return $p;
	}

	public static function api_set($id) {
		$p = new Product($id);

		$data = [];
		foreach ($_POST as $k=>$v) {
			$p->$k = $v;
		}
		$p->save();

		return $p;
	}

	public static function api_move($id) {
		$p = new Product($id);
		$p->parent = $_POST['to'];
		$p->save();
		return $p;
	}

	public static function api_delete($id) {
		$product = new Product($id);
		if ($product->valid()) {
			$laf = Product::find([["title", "=", "Lost and Found"]])->first();

			DB::getInstance()->query("delete from docproduct where product=:pid", ["pid" => $product->id]);

			foreach ($product->children as $c) {
				$c->parent = $laf;
				$c->save();
			}
			$product->load("parent");
			$par = $product->parent;
			$product->delete();
			return $par;
		}
		return [];
	}

	public static function api_empty_trash($id) {
		$prod = new Product($id);
		$docs = $prod->documents;

		foreach ($docs as $doc) {
			$revs = $doc->revisions;
			foreach ($revs as $rev) {
				$rev->delete();
			}

			$dpl = DocProduct::find([["document", "=", $doc->id]])->all();
			foreach ($dpl as $dp) {
				$dp->delete();
			}

			$doc->delete();
		}

		$prod->invalidate("documents");


		return [];

	}

    public static function api_available_metadata($id) {
		$prod = new Product($id);
        $exist = $prod->meta();
        $metas = MetaType::find()->all();

        $out = new Collection();

        foreach ($metas as $meta) {
			if (!in_array($meta->id, $exist)) {
                $cl = new stdClass;
                $cl->key = $meta->id;
                $cl->value = $meta->name;
                $out->push($cl);
            }
        }

        $out->sort("value");

        return $out;
    }

	public static function api_add_metadata($_request, $id) {
		$prod = new Product($id);
		$prod->add_meta($_request->put('item_id'));
		return Collection::from_array($prod->meta());
	}

	public static function api_gemini_all($id) {
		$prod = new Product($id);

		$c = new Collection();

		foreach ($prod->documents as $doc) {
			$job = new GeminiJob($doc->id, "document:" . $doc->id);
			$jobid = $job->queue();
			$c->push(["document" => $doc->id, "job" => $jobid]);
		}
		return $c;
	}
}
