<?php
    /**
     * Team entity file.
     *
     * Creates the entity and makes it available.
     *
     * @category Entity
     */

    /**
     * Require dependent entities if not present.
     */
    require_once(PATH::ENTITY . "K_Area.php");
    require_once(PATH::ENTITY . "Unit.php");

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

        /**
         * @var int Owner identifier.
         */
        public $uid;

        /**
         * @var int Team identifier.
         */
        public $id;

        /**
         * @var string Team name.
         */
        public $name;

        /**
         * @var string Team description.
         */
        public $description;

        /**
         * @var \Unit[] Units in the team.
         */
        public $unit = [];

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

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

        /**
         * @var int difficulty the team is designed for. It can be null.
         *
         * @see DIFFICULTY
         */
        public $difficulty;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * monster, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Monster identifier.
         * @param bool $complete If false, it won't load the units.
         */
        public function __construct($db, $id, $complete = true){
            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 = HTML::e($r["name"]);
            $this->description = HTML::e($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));
                }
            }
        }

    }
?>
