<?php

namespace Lucent\Schema;

use Lucent\LucentException;
use Lucent\Primitive\Collection;

class SchemaService
{


    public function __construct()
    {
    }

    public function fromArray(array $schemaArr): Schema
    {

        return match ($schemaArr["type"]) {
            "collection" => new CollectionSchema(
                name: $schemaArr["name"],
                label: $schemaArr["label"],
                visible: $schemaArr["visible"] ?? [],
                groups: $schemaArr["groups"] ?? [],
                fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
                isEntry: $schemaArr["isEntry"] ?? false,
                color: $schemaArr["color"] ?? "",
                sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
                cardTitle: $schemaArr["titleTemplate"] ?? $schemaArr["cardTitle"] ?? null,
                cardImage: $schemaArr["cardImage"] ?? null,
                revisions: $schemaArr["revisions"] ?? 0,
                read: $schemaArr["read"] ?? [],
                write: $schemaArr["write"] ?? [],
            ),
            "files" => new FilesSchema(
                name: $schemaArr["name"],
                label: $schemaArr["label"],
                fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
                disk: $schemaArr["disk"] ?? "lucent",
                path: $schemaArr["path"] ?? $schemaArr["name"],
                groups: $schemaArr["groups"] ?? [],
                isEntry: $schemaArr["isEntry"] ?? false,
                sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
                color: $schemaArr["color"] ?? "",
                cardTitle: $schemaArr["titleTemplate"] ?? $schemaArr["cardTitle"] ?? null,
                cardImage: $schemaArr["cardImage"] ?? null,
                revisions: $schemaArr["revisions"] ?? 0,
                read: $schemaArr["read"] ?? [],
                write: $schemaArr["write"] ?? [],
            )
        };

    }

    public function mapFields(array $field): FieldInterface
    {

        $schemaFields = [
            \Lucent\Schema\Ui\Checkbox::class,
            \Lucent\Schema\Ui\Color::class,
            \Lucent\Schema\Ui\Date::class,
            \Lucent\Schema\Ui\Datetime::class,
            \Lucent\Schema\Ui\File::class,
            \Lucent\Schema\Ui\Json::class,
            \Lucent\Schema\Ui\Markdown::class,
            \Lucent\Schema\Ui\Number::class,
            \Lucent\Schema\Ui\Reference::class,
            \Lucent\Schema\Ui\Rich::class,
            \Lucent\Schema\Ui\Slug::class,
            \Lucent\Schema\Ui\Text::class,
            \Lucent\Schema\Ui\Textarea::class,
        ];
        $ui = collect($schemaFields)->filter(function ($className) use ($field) {
            return str_ends_with(strtolower($className), "\\" . strtolower($field["ui"]));
        })->first();

        if (empty($ui)) {
            throw new LucentException("Field UI " . $field["ui"] . " not found");
        }

        unset($field["ui"]);
        return new $ui(...$field);
    }

}
