<?php
/**
 * Content shortcut ability schemas.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks\Schemas;

use LolaCoreForBricks\AbilitiesProvider;

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

        $page = [
            'post_id' => [
                'type'        => 'integer',
                'description' => 'WordPress post or page ID.',
            ],
            'area'    => [
                'type'        => 'string',
                'description' => 'Page area: content, header, or footer.',
            ],
        ];

        $placement = [
            'parent_id'    => [
                'type'        => 'string',
                'description' => 'Parent element ID. Empty inserts at the root of the area.',
            ],
            'position'     => [
                'type'        => 'string',
                'description' => 'Placement: child, before, or after.',
            ],
            'reference_id' => [
                'type'        => 'string',
                'description' => 'Sibling element ID when position is before or after.',
            ],
        ];

        $shortcutExtras = [
            'content' => [
                'type'        => 'object',
                'description' => 'Text, link, image, or heading tag for the new element.',
            ],
            'classes' => [
                'type'        => 'array',
                'items'       => ['type' => 'string'],
                'description' => 'Existing Bricks global class names or IDs to apply.',
            ],
            'styles'  => [
                'type'        => 'object',
                'description' => 'Optional universal style controls. Loose values that match a site variable are noted.',
            ],
        ];

        $shortcuts = [];
        foreach ([
            'addSection'   => 'Insert a Bricks section with optional content and site classes in one confirmation. Call with dry_run first.',
            'addContainer' => 'Insert a Bricks container with optional content and site classes in one confirmation. Call with dry_run first.',
            'addDiv'       => 'Insert a Bricks div with optional content and site classes in one confirmation. Call with dry_run first.',
            'addHeading'   => 'Insert a Bricks heading with its text and site classes already set. Call with dry_run first.',
            'addText'      => 'Insert a Bricks text element with its copy and site classes already set. Call with dry_run first.',
            'addImage'     => 'Insert a Bricks image from the media library. External URLs are rejected. Call with dry_run first.',
            'addButton'    => 'Insert a Bricks button with its label, link, and site classes already set. Call with dry_run first.',
        ] as $name => $description) {
            $shortcuts[$name] = [
                'description' => $description,
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => [strtolower((string) preg_replace('/[A-Z]/', ' $0', $name))],
                    'stem'  => ['add', 'insert', 'content'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $page, $placement, $shortcutExtras),
                    'required'   => ['post_id'],
                ],
            ];
        }

        return array_merge([
            'setElementContent' => [
                'description' => 'Update text, image, or link content on one or more existing Bricks elements in one confirmation. Call with dry_run first.',
                'category'    => AbilitiesProvider::CATEGORY,
                'keywords'    => [
                    'exact' => ['set content', 'change text', 'cambiar texto', 'cambiar contenido'],
                    'stem'  => ['content', 'text', 'contenido'],
                ],
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => array_merge($dryRun, $page, [
                        'element_id' => ['type' => 'string'],
                        'content'    => ['type' => 'object'],
                        'items'      => [
                            'type'  => 'array',
                            'items' => ['type' => 'object'],
                        ],
                    ]),
                    'required'   => ['post_id'],
                ],
            ],
        ], $shortcuts);
    }
}
