<?php

namespace WPUltimateSecurity\UltimateSecurity\Infrastructure\Container;

use Exception;

/**
 * Container Exception
 *
 * Thrown when there is an error resolving a service from the container.
 *
 * @package WPUltimateSecurity\UltimateSecurity\Infrastructure\Container
 * @since 2.0.0
 */
class ContainerException extends Exception
{
    /**
     * Create an exception for a resolution error.
     *
     * @param string    $id       The service identifier being resolved.
     * @param Exception $previous The original exception.
     *
     * @return self
     */
    public static function forResolutionError(string $id, Exception $previous): self
    {
        return new self(
            sprintf(
                'Error while resolving service "%s": %s',
                $id,
                $previous->getMessage()
            ),
            0,
            $previous
        );
    }

    /**
     * Create an exception for a circular dependency.
     *
     * @param string $id    The service identifier with circular dependency.
     * @param array  $chain The resolution chain showing the circular path.
     *
     * @return self
     */
    public static function forCircularDependency(string $id, array $chain): self
    {
        return new self(
            sprintf(
                'Circular dependency detected for service "%s". Resolution chain: %s',
                $id,
                implode(' -> ', $chain)
            )
        );
    }
}
