<?php

declare(strict_types=1);

namespace App\Framework\Exception;

final class DirectoryListException extends FrameworkException
{
    public function __construct(
        string $directory,
        int $code = 0,
        ?\Throwable $previous = null
    ) {
        $context = ExceptionContext::forOperation('filesystem.list_directory', 'FileSystem')
            ->withData([
                'directory' => $directory,
                'exists' => is_dir($directory),
                'readable' => is_readable($directory),
                'permissions' => is_dir($directory) ? decoct(fileperms($directory)) : 'unknown',
                'file_count' => is_readable($directory) ? count(scandir($directory)) - 2 : 'unknown',
            ]);

        parent::__construct(
            message: "Fehler beim Auslesen des Verzeichnisses '$directory'.",
            code: $code,
            previous: $previous,
            context: $context
        );
    }
}
