<?php
require_once 'core.php';
require_once 'db.php';
require_once 'user.php';
require_once 'post.php';

class Space
{
    private int $id;
    private string $name;
    private string $description;
    private int $visibility;
    private array $members;
    private array $posts;
    private ?Space $parent;

    public function __construct(?int $id, string $name, string $description, int $visibility, array $members, array $posts, ?Space $parent = null)
    {
        $this->id = $id ?? randomId(2);
        $this->name = $name;
        $this->description = $description;
        $this->visibility = $visibility;
        $this->members = $members;
        $this->posts[] = $posts;
        $this->parent = $parent;

        /* $stmt = 'INSERT INTO spaces (spaceId, name, description, parentSpaceIdk)'; */
    }

    public function getCardHTML(): string
    {
        $html = '
            <div class="full_width std_border padding center top_margin bottom_margin">
                <a href="spaces.php?view=show&id=' . $this->id . '" class="divLink">
                    <div class="row space_between full_width">
                        <div class="column std_width">
                            <h4>' . $this->name . '</h4>
                        </div>
                        
                        <div class="">
                            <span>Something</span>
                        </div>
                    </div>
                    <div class="row full_width">
                        <p>' . $this->description . '</p>
                    </div>
                </a>
            </div>
        ';

        return $html;
    }

    public static function retrieveFromDB(int $space, int $user = -1): ?Space
    {
        if ($user != -1) {
            $stmt = 'SELECT * FROM spaceMembers WHERE space = ? AND user = ?';
            $isMember = exec_stmt($stmt, 'ii', $space, $user)->fetch_assoc();
            if (!$isMember)
                return null;
        } else {
            $stmt = 'SELECT visibility FROM space WHERE id = ?';
            $isOpen = exec_stmt($stmt, 'i', $space);
            if (!$isOpen)
                return null;
        }

        $stmt = 'SELECT * FROM space WHERE id = ?';
        $rawSpace = exec_stmt($stmt, 'i', $space)->fetch_assoc();

        if (!empty($rawSpace['parentSpace']))
            $parent = Space::retrieveFromDB($rawSpace['parentSpace'], $user);
        else
            $parent = null;

        $stmt = 'SELECT user FROM spaceMembers WHERE space = ?';
        $members = [];
        $rawMembers = exec_stmt($stmt, 'i', $rawSpace['id']);
        foreach ($rawMembers as $m) {
            $members[] = User::retrieveFromDB($m['user']);
        }

        $stmt = 'SELECT post FROM spacePosts WHERE space = ?';
        $posts = [];
        $rawPosts = exec_stmt($stmt, 'i', $rawSpace['id']);
        while ($row = $rawPosts->fetch_assoc()) {
            $posts[] = Post::retrieveFromDB($row['post']);
        }

        $space = new Space($rawSpace['id'], $rawSpace['name'], $rawSpace['description'], $rawSpace['visibility'], $members, $posts, $parent);

        foreach ($posts as $p)
            $p->setSpace($space);

        return $space;
    }

    public function allowUser(User $user)
    {
        $stmt = 'SELECT space FROM spaceMembers WHERE user = ?';
        $result = exec_stmt($stmt, 'i', $user->getId())->fetch_assoc();
        if ($result['space'] == $this->id)
            return true;
        else
            return false;
    }

    public function getPosts()
    {
        return $this->posts;
    }

    public function __toString()
    {
        echo 'todo: this should be JSON';
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getDescription(): string
    {
        return $this->description;
    }

    public function getVisibility(): int
    {
        return $this->visibility;
    }
}
