<?php

namespace Shm\ShmChat\Classes;

class CreateChannelInput
{
    public string $space = '';
    public string $name = '';
    public string $type = 'default';
    /** @var array<string, mixed> */
    public array $payload = [];
    public bool $closed = false;

    /** @var list<ChatMember> */
    public array $members = [];

    /** @var list<ChannelReference> */
    public array $references = [];

    /**
     * @param list<ChatMember> $members
     */
    public static function make(array $members, ?string $space = null, ?string $name = null, string $type = 'default'): self
    {
        $input = new self();
        $input->members = $members;
        $input->space = $space ?? '';
        $input->name = $name ?? '';
        $input->type = $type;

        return $input;
    }

    public function addReference(ChannelReference $ref): self
    {
        foreach ($this->references as $existing) {
            if (
                $existing->documentCollection === $ref->documentCollection
                && $existing->documentIdKey() === $ref->documentIdKey()
            ) {
                return $this;
            }
        }

        $this->references[] = $ref;

        return $this;
    }
}
