<?php
/**
 * Canonical map for B12 component write abilities across Doors 2 and 3.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks;

/**
 * Single registry for component write action metadata.
 */
final class ComponentWriteAbilityMap
{
    /**
     * @return array<string, array{handler: string, slug: string, label: string, description: string, destructive: bool, idempotent: bool}>
     */
    public static function entries(): array
    {
        return [
            'create_component_from_element' => [
                'handler'     => 'createComponentFromElement',
                'slug'        => 'lolacore-bricks/create-component-from-element',
                'label'       => 'Create Component From Element',
                'description' => 'Convert a Bricks element branch into a component and leave an instance in place. Call with dry_run first.',
                'destructive' => false,
                'idempotent'  => false,
            ],
            'update_component_properties' => [
                'handler'     => 'updateComponentProperties',
                'slug'        => 'lolacore-bricks/update-component-properties',
                'label'       => 'Update Component Properties',
                'description' => 'Define or edit Bricks component properties and their types. Always previews instance count. Call with dry_run first.',
                'destructive' => false,
                'idempotent'  => true,
            ],
            'update_component' => [
                'handler'     => 'updateComponent',
                'slug'        => 'lolacore-bricks/update-component',
                'label'       => 'Update Component',
                'description' => 'Edit a Bricks component structure or style. Always previews how many instances change. Call with dry_run first.',
                'destructive' => false,
                'idempotent'  => true,
            ],
            'insert_component_instance' => [
                'handler'     => 'insertComponentInstance',
                'slug'        => 'lolacore-bricks/insert-component-instance',
                'label'       => 'Insert Component Instance',
                'description' => 'Insert a Bricks component instance with property values. Call with dry_run first.',
                'destructive' => false,
                'idempotent'  => false,
            ],
            'update_component_instance' => [
                'handler'     => 'updateComponentInstance',
                'slug'        => 'lolacore-bricks/update-component-instance',
                'label'       => 'Update Component Instance',
                'description' => 'Change property values on one Bricks component instance. Call with dry_run first.',
                'destructive' => false,
                'idempotent'  => true,
            ],
            'detach_component_instance' => [
                'handler'     => 'detachComponentInstance',
                'slug'        => 'lolacore-bricks/detach-component-instance',
                'label'       => 'Detach Component Instance',
                'description' => 'Turn a Bricks component instance into regular elements on that page. Call with dry_run first.',
                'destructive' => false,
                'idempotent'  => false,
            ],
        ];
    }

    /**
     * @return array<string, array{requires_confirmation: bool, type: string}>
     */
    public static function doorTwoActions(): array
    {
        $actions = [];
        foreach (array_keys(self::entries()) as $actionId) {
            $actions[$actionId] = [
                'requires_confirmation' => true,
                'type'                  => 'write',
            ];
        }

        return $actions;
    }

    /**
     * @return string[]
     */
    public static function doorThreeSlugs(): array
    {
        return array_values(array_map(
            static fn (array $entry): string => $entry['slug'],
            self::entries()
        ));
    }
}
