<?php

declare(strict_types=1);

namespace App\Framework\Discovery\ValueObjects;

use App\Framework\Core\ValueObjects\ClassName;
use App\Framework\Filesystem\File;
use App\Framework\Filesystem\ValueObjects\FilePath;

/**
 * Context information for file processing
 */
final readonly class FileContext
{
    /** @var array<ClassName> */
    private array $classNames;

    public function __construct(
        public File $file,
        public FilePath $path,
        array $classNames = []
    ) {
        $this->classNames = $classNames;
    }

    public static function fromFile(File $file): self
    {
        return new self(
            file: $file,
            path: $file->getPath(),
            classNames: []
        );
    }

    /**
     * @param array<ClassName> $classNames
     */
    public function withClassNames(array $classNames): self
    {
        return new self(
            file: $this->file,
            path: $this->path,
            classNames: $classNames
        );
    }

    /**
     * @return array<ClassName>
     */
    public function getClassNames(): array
    {
        return $this->classNames;
    }

    public function hasClasses(): bool
    {
        return ! empty($this->classNames);
    }

    public function getNamespace(): ?string
    {
        if (empty($this->classNames)) {
            return null;
        }

        return $this->classNames[0]->getNamespace();
    }

    /**
     * Get all fully qualified class names
     * @return array<string>
     */
    public function getFullyQualifiedNames(): array
    {
        return array_map(
            fn (ClassName $className) => $className->getFullyQualified(),
            $this->classNames
        );
    }
}
