<?php

    require_once($path["entity"] . "K_Area.php");
    require_once($path["entity"] . "Unit.php");


    /**
     * A Team.
     *
     * Represents an object from the table 'unit'.
     */
    class Team extends Entity{

        /**
         * Owner identifier.
         */
        public $uid;

        /**
         * Team identifier.
         */
        public $id;

        /**
         * Team name.
         */
        public $name;

        /**
         * Team description.
         */
        public $description;

        /**
         * List of {@see Unit}s in the team.
         */
        public $unit = [];

        /**
         * Area the team is designed for.
         */
        public $area;

        /**
         * Stage the team is designed for. It can be null.
         */
        public $stage;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * monster, populating it and it's items.
         *
         * @param SQLite3 $db Connection to the database.
         * @param int $id Monster identifier.
         * @param complete If false, query only monster and k_monster.
         */
        public function __construct($db, $id, $complete = true){
            global $path;
            parent::__construct($db);
            $s = "
              SELECT
                uid,
                id,
                name,
                description,
                area_type,
                area,
                stage,
                difficulty
              FROM team
              WHERE id = '$id';
            ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            $this->uid = $r["uid"];
            $this->id = $r["id"];
            $this->name = $r["name"];
            $this->description = $r["description"];
            $this->area = new K_Area($this->db, $r["area"], $r["area_type"]);
            $this->stage = $r["stage"];
            if ($complete){
                $s = "
                  SELECT unit
                  FROM team_unit
                  WHERE team = '$id';
                ";
                $q = $this->db->query($s);
                while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                    array_push($this->unit, new Unit($this->db, $r["unit"], false));
                }
            }
        }

    }
?>
