<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Db;

use JsonSerializable;
use OCP\AppFramework\Db\Entity;

/**
 * Saved query entity mapping columns from oc_mv_saved_queries.
 *
 * @method string getName()
 * @method void setName(string $name)
 * @method string getQueryJson()
 * @method void setQueryJson(string $queryJson)
 * @method string|null getSortJson()
 * @method void setSortJson(?string $sortJson)
 * @method string getCreatedBy()
 * @method void setCreatedBy(string $createdBy)
 * @method string getCreatedAt()
 * @method void setCreatedAt(string $createdAt)
 *
 * Part of Issue #53.
 */
class SavedQuery extends Entity implements JsonSerializable {

    protected string $name = '';
    protected string $queryJson = '';
    protected ?string $sortJson = null;
    protected string $createdBy = '';
    protected string $createdAt = '';

    public function __construct() {
        $this->addType('id', 'integer');
    }

    public function jsonSerialize(): array {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'queryJson' => json_decode($this->queryJson, true),
            'sortJson' => $this->sortJson ? json_decode($this->sortJson, true) : null,
            'createdBy' => $this->createdBy,
            'createdAt' => $this->createdAt,
        ];
    }
}
