<?php

namespace Lucent\Channel;

use Lucent\Channel\Data\UserCommand;
use Lucent\Primitive\Collection;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema;

final class Channel
{
    public string $lucentUrl;
    public string $filesUrl;
    public array $disks;

    /**
     * @param Collection<Schema> $schemas
     * @param Collection<UserCommand> $commands
     */
    function __construct(
        public string     $name,
        public string     $url,
        public string     $previewTarget,
        public Collection $commands,
        public Collection $schemas,
        public array      $imageFilters,
        public array      $roles,
    )
    {
        $this->lucentUrl = $url . "/lucent";
        $this->filesUrl = $this->makeFilesUrl();
        $this->disks = $this->getDisksFromSchemas();
        $this->previewTarget = $url . "/" . $previewTarget;
    }


    private function makeFilesUrl(): string
    {
        return match (config("filesystems.disks.lucent.driver")) {
            "s3" => config("filesystems.disks.lucent.endpoint") . "/" . config("filesystems.disks.lucent.bucket"),
            "local" => $this->url . "/storage" . config("filesystems.disks.lucent.endpoint"),
            default => ""
        };

    }

    private function getDisksFromSchemas()
    {
        return $this->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->reduce(function (array $carry, Schema $schema) {
            $carry[$schema->disk] = config("filesystems.disks." . $schema->disk . ".url");
            return $carry;
        }, []);
    }
}
