<?php

declare(strict_types=1);

namespace OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Builder;

use OCA\Talk\Vendor\CuyZ\Valinor\Definition\FunctionDefinition;
use OCA\Talk\Vendor\CuyZ\Valinor\Definition\FunctionsContainer;
use OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Exception\InvalidAbstractObjectName;
use OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Exception\InvalidResolvedImplementationValue;
use OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Exception\MissingObjectImplementationRegistration;
use OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Exception\ObjectImplementationCallbackError;
use OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Exception\ObjectImplementationNotRegistered;
use OCA\Talk\Vendor\CuyZ\Valinor\Mapper\Tree\Exception\ResolvedImplementationIsNotAccepted;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\ClassType;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\Parser\Exception\InvalidType;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\Parser\TypeParser;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\Type;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\Types\ClassStringType;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\Types\InterfaceType;
use OCA\Talk\Vendor\CuyZ\Valinor\Type\Types\UnionType;
use Exception;

/** @internal */
final class ObjectImplementations
{
    /** @var array<string, non-empty-array<string, ClassType>> */
    private array $implementations = [];

    public function __construct(
        private FunctionsContainer $functions,
        private TypeParser $typeParser
    ) {
        foreach ($functions as $name => $function) {
            /** @var string $name */
            $this->implementations[$name] = $this->implementations($name);
        }
    }

    public function has(string $name): bool
    {
        return $this->functions->has($name);
    }

    public function function(string $name): FunctionDefinition
    {
        return $this->functions->get($name)->definition();
    }

    /**
     * @param mixed[] $arguments
     */
    public function implementation(string $name, array $arguments): ClassType
    {
        $class = $this->call($name, $arguments);

        return $this->implementations[$name][$class]
            ?? throw new ObjectImplementationNotRegistered($class, $name, $this->implementations[$name]);
    }

    /**
     * @param mixed[] $arguments
     */
    private function call(string $name, array $arguments): string
    {
        try {
            $signature = ($this->functions->get($name)->callback())(...$arguments);
        } catch (Exception $exception) {
            throw new ObjectImplementationCallbackError($name, $exception);
        }

        if (! is_string($signature)) {
            throw new InvalidResolvedImplementationValue($name, $signature);
        }

        return $signature;
    }

    /**
     * @return non-empty-array<string, ClassType>
     */
    private function implementations(string $name): array
    {
        $function = $this->functions->get($name)->definition();

        try {
            $type = $this->typeParser->parse($name);
        } catch (InvalidType) {
        }

        if (! isset($type) || (! $type instanceof InterfaceType && ! $type instanceof ClassType)) {
            throw new InvalidAbstractObjectName($name);
        }

        $classes = $this->implementationsByReturnSignature($name, $function);

        if (empty($classes)) {
            throw new MissingObjectImplementationRegistration($name, $function);
        }

        foreach ($classes as $classType) {
            if (! $classType instanceof ClassType || ! $classType->matches($type)) {
                throw new ResolvedImplementationIsNotAccepted($name, $classType);
            }
        }

        /** @var non-empty-array<string, ClassType> $classes */
        return $classes;
    }

    /**
     * @return array<string, Type>
     */
    private function implementationsByReturnSignature(string $name, FunctionDefinition $function): array
    {
        $returnType = $function->returnType();

        if (! $returnType instanceof ClassStringType && ! $returnType instanceof UnionType) {
            if (count($function->parameters()) > 0) {
                return [];
            }

            $class = $this->call($name, []);
            $classType = $this->typeParser->parse($class);

            return [$classType->toString() => $classType];
        }

        $types = $returnType instanceof UnionType
            ? $returnType->types()
            : [$returnType];

        $classes = [];

        foreach ($types as $type) {
            if (! $type instanceof ClassStringType) {
                return [];
            }

            $subType = $type->subType();

            if ($subType === null) {
                return [];
            }

            $subTypes = $subType instanceof UnionType
                ? $subType->types()
                : [$subType];

            foreach ($subTypes as $classType) {
                $classes[$classType->toString()] = $classType;
            }
        }

        return $classes;
    }
}
