<?php
/**
 * Component write ability schemas.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks\Schemas;

use LolaCoreForBricks\AbilitiesProvider;

/**
 * Door 1 schemas for B12 component writes.
 */
class ComponentWriteSchemas
{
    /**
     * @return array<string, array<string, mixed>>
     */
    public static function define(): array
    {
        $dryRun = [
            'dry_run' => [
                'type'        => 'boolean',
                'description' => 'Preview the write without saving changes.',
            ],
        ];

        $component = [
            'component' => [
                'type'        => 'string',
                'description' => 'Component id or name.',
            ],
        ];

        $page = [
            'post_id' => ['type' => 'integer'],
            'area'    => ['type' => 'string'],
        ];

        return [
            'createComponentFromElement' => [
                'description' => 'Convert a Bricks element branch into a reusable component and leave an instance in its place. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['create component', 'convert to component', 'crear componente'],
                    'stem'  => ['component', 'componente'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $page, [
                        'element_id' => ['type' => 'string'],
                        'name'       => ['type' => 'string'],
                    ]),
                    'required'   => ['post_id', 'element_id'],
                ],
            ],
            'updateComponentProperties' => [
                'description' => 'Define or edit Bricks component properties and types. Always previews how many instances change. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['component properties', 'propiedades de componente'],
                    'stem'  => ['property', 'component', 'propiedad'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $component, [
                        'properties' => ['type' => 'array', 'items' => ['type' => 'object']],
                    ]),
                    'required'   => ['component', 'properties'],
                ],
            ],
            'updateComponent' => [
                'description' => 'Edit a Bricks component structure or style. Always previews how many instances change. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['update component', 'edit component', 'editar componente'],
                    'stem'  => ['component', 'componente'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $component, [
                        'name'     => ['type' => 'string'],
                        'elements' => ['type' => 'array', 'items' => ['type' => 'object']],
                    ]),
                    'required'   => ['component'],
                ],
            ],
            'insertComponentInstance' => [
                'description' => 'Insert a Bricks component instance with optional property values. Warns at the nesting limit of 10. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['insert component', 'add instance', 'insertar componente'],
                    'stem'  => ['instance', 'component', 'instancia'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $component, $page, [
                        'parent_id'    => ['type' => 'string'],
                        'reference_id' => ['type' => 'string'],
                        'position'     => ['type' => 'string'],
                        'properties'   => ['type' => 'object'],
                    ]),
                    'required'   => ['component', 'post_id'],
                ],
            ],
            'updateComponentInstance' => [
                'description' => 'Change property values on one Bricks component instance. Other instances keep their own values. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['update instance', 'instance properties', 'actualizar instancia'],
                    'stem'  => ['instance', 'property', 'instancia'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $page, [
                        'element_id'  => ['type' => 'string'],
                        'instance_id' => ['type' => 'string'],
                        'properties'  => ['type' => 'object'],
                    ]),
                    'required'   => ['post_id', 'properties'],
                ],
            ],
            'detachComponentInstance' => [
                'description' => 'Turn a Bricks component instance into regular elements on that page only. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['detach component', 'unlink instance', 'desvincular componente'],
                    'stem'  => ['detach', 'unlink', 'desvincular'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $page, [
                        'element_id'  => ['type' => 'string'],
                        'instance_id' => ['type' => 'string'],
                    ]),
                    'required'   => ['post_id'],
                ],
            ],
        ];
    }
}
