<?php

declare(strict_types=1);

namespace App\Framework\Filesystem\Exceptions;

use App\Framework\Exception\Core\FilesystemErrorCode;
use App\Framework\Exception\ExceptionContext;

final class FilePermissionException extends FilesystemException
{
    public function __construct(
        string $path,
        string $operation = 'access',
        ?string $reason = null
    ) {
        $message = "Permission denied for {$operation} on file: {$path}";

        if ($reason) {
            $message .= " ({$reason})";
        }

        $context = ExceptionContext::forOperation('file.permission', 'filesystem')
            ->withData([
                'path' => $path,
                'operation' => $operation,
                'reason' => $reason,
            ]);

        parent::__construct(
            message: $message,
            context: $context,
            errorCode: FilesystemErrorCode::PERMISSION_DENIED
        );
    }

    public static function read(string $path, ?string $reason = null): self
    {
        return new self($path, 'read', $reason);
    }

    public static function write(string $path, ?string $reason = null): self
    {
        return new self($path, 'write', $reason);
    }

    public static function delete(string $path, ?string $reason = null): self
    {
        return new self($path, 'delete', $reason);
    }

    public static function createDirectory(string $path, ?string $reason = null): self
    {
        return new self($path, 'create directory', $reason);
    }
}
