<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Db;

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

/**
 * @method string getNcUserId()
 * @method void setNcUserId(string $ncUserId)
 * @method string getLevel()
 * @method void setLevel(string $level)
 * @method string|null getAllowedStufenJson()
 * @method void setAllowedStufenJson(?string $allowedStufenJson)
 * @method bool getCanSeeBanking()
 * @method void setCanSeeBanking(bool $canSeeBanking)
 */
class Permission extends Entity implements JsonSerializable {

    protected string $ncUserId = '';
    protected string $level = 'none';
    protected ?string $allowedStufenJson = null;
    protected bool $canSeeBanking = false;

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

    /**
     * Get allowed Stufe IDs as an array.
     * @return int[]
     */
    public function getAllowedStufen(): array {
        if ($this->allowedStufenJson === null) {
            return [];
        }
        $decoded = json_decode($this->allowedStufenJson, true);
        return is_array($decoded) ? array_map('intval', $decoded) : [];
    }

    public function jsonSerialize(): array {
        return [
            'id' => $this->id,
            'ncUserId' => $this->ncUserId,
            'level' => $this->level,
            'allowedStufen' => $this->getAllowedStufen(),
            'canSeeBanking' => $this->canSeeBanking,
        ];
    }
}
