<?php

declare(strict_types=1);

namespace App\Framework\MagicLinks\Actions;

use App\Framework\MagicLinks\MagicLinkData;
use App\Framework\MagicLinks\TokenConfig;
use App\Framework\MagicLinks\ValueObjects\ActionResultData;

final readonly class GenericDataAccessAction implements MagicLinkAction
{
    public function __construct(
        private string $actionName,
        private TokenConfig $config,
        private array $requiredPermissions = []
    ) {
    }

    public function getName(): string
    {
        return $this->actionName;
    }

    public function getDefaultConfig(): TokenConfig
    {
        return $this->config;
    }

    public function validatePayload(array $payload): bool
    {
        // Generic validation - just check that payload is not empty
        return ! empty($payload);
    }

    public function execute(MagicLinkData $magiclinkData, array $context = []): ActionResult
    {
        // Generic execution - just return the payload data
        return ActionResult::success(
            message: "Action '{$this->actionName}' executed successfully",
            data: ActionResultData::fromArray($magiclinkData->payload->toArray())
        );
    }

    public function getRequiredPermissions(): array
    {
        return $this->requiredPermissions;
    }
}
