<?php

namespace App\Framework\Smartlinks\Actions;

use App\Framework\Smartlinks\SmartlinkData;
use App\Framework\Smartlinks\TokenConfig;

final readonly class DocumentAccessAction implements SmartlinkAction
{
    public function getName(): string
    {
        return 'document_access';
    }

    public function getDefaultConfig(): TokenConfig
    {
        return new TokenConfig(
            expiryHours: 72, // 3 days
            oneTimeUse: false, // Can be accessed multiple times
            maxUses: 10,
            requireSecureContext: false
        );
    }

    public function validatePayload(array $payload): bool
    {
        return !empty($payload['document_id']) &&
            in_array($payload['access_level'], ['read', 'download', 'edit']);
    }

    public function execute(SmartlinkData $smartlinkData, array $context = []): ActionResult
    {
        $payload = $smartlinkData->payload;

        // Document access logic
        return ActionResult::success(
            message: "Document access granted",
            data: [
                'document_id' => $payload['document_id'],
                'access_level' => $payload['access_level'],
                'download_url' => "/documents/{$payload['document_id']}/download",
                'expires_at' => $smartlinkData->expiresAt->format('Y-m-d H:i:s')
            ]
        );
    }

    public function getRequiredPermissions(): array
    {
        return ['documents.access'];
    }
}
