<?php

declare(strict_types=1);

namespace Qameta\Allure\Model;

use Ramsey\Uuid\UuidFactoryInterface;
use Qameta\Allure\Io\ClockInterface;
use DateTimeImmutable;

final class ResultFactory implements ResultFactoryInterface
{
    public function __construct(
        private UuidFactoryInterface $uuidFactory,
        private ClockInterface $clock,
    ) {
    }

    public function createContainer(): ContainerResult
    {
        return new ContainerResult($this->createUuid());
    }

    public function createTest(): TestResult
    {
        return new TestResult($this->createUuid());
    }

    public function createStep(): StepResult
    {
        return new StepResult($this->createUuid());
    }

    public function createFixture(): FixtureResult
    {
        return new FixtureResult($this->createUuid());
    }

    public function createAttachment(): AttachmentResult
    {
        return new AttachmentResult($this->createUuid());
    }

    public function createGlobalError(): GlobalError
    {
        return new GlobalError(
            timestamp: $this->now(),
        );
    }

    public function createGlobalAttachment(): GlobalAttachment
    {
        return new GlobalAttachment(
            uuid: $this->createUuid(),
            timestamp: $this->now(),
        );
    }

    public function createGlobals(): Globals
    {
        return new Globals(uuid: $this->createUuid());
    }

    private function createUuid(): string
    {
        return $this
            ->uuidFactory
            ->uuid4()
            ->toString();
    }

    private function now(): DateTimeImmutable
    {
        return $this->clock->now();
    }
}
