<?php

namespace EasyLinkAutomations;

if (!defined('ABSPATH')) {
    exit;
}

use EasyLinkAutomations\Helpers\BrandHelper;
use EasyLinkAutomations\Views\Body;

/**
 * Defines the WordPress admin sidebar menu structure.
 *
 * Returns an array of menu/submenu definitions consumed by Layout::registerMenus().
 * Each entry follows this shape:
 *
 * [
 *   'type'       => 'menu' | 'submenu',
 *   'name'       => 'Visible label',
 *   'capability' => 'manage_options',
 *   'slug'       => 'page slug',
 *   // menu-only:
 *   'title'      => 'Browser tab title',
 *   'callback'   => callable,
 *   'icon'       => 'dashicons-* or data URI',
 *   'position'   => 25,
 *   // submenu-only:
 *   'parent'     => 'parent menu slug',
 * ]
 */
final class Menu
{
    /**
     * @return array<string, array<string, mixed>>
     */
    public static function getSideBarMenu(Body $body): array
    {
        return [
            'Home'        => self::homeMenu($body),
            'Dashboard'   => self::submenu(__('Dashboard', 'easylink-automations'), '#/'),
            'Automations' => self::submenu(__('Automations', 'easylink-automations'), '#/automations'),
            'Connections' => self::submenu(__('Connections', 'easylink-automations'), '#/connections'),
            'Logs'        => self::submenu(__('Logs', 'easylink-automations'), '#/logs'),
            'Settings'    => self::submenu(__('Settings', 'easylink-automations'), '#/settings'),
        ];
    }

    // ──────────────────────────────────────────────
    //  Menu builders
    // ──────────────────────────────────────────────

    private static function homeMenu(Body $body): array
    {
        $title = Config::title();

        return [
            'type'       => 'menu',
            'title'      => sprintf(
                /* translators: %s: plugin display name. */
                __('%s - Automation Plugin', 'easylink-automations'),
                $title
            ),
            'name'       => $title,
            'capability' => 'manage_options',
            'slug'       => Config::SLUG,
            'callback'   => [$body, 'render'],
            'icon'       => self::menuIconSvg(),
            'position'   => '25',
        ];
    }

    /**
     * Base64-encoded SVG icon for the admin sidebar menu.
     *
     * Monochrome so it picks up the admin color scheme's own opacity states.
     */
    private static function menuIconSvg(): string
    {
        return BrandHelper::toDataUri(BrandHelper::monoSvg());
    }

    /**
     * @param string $name Already-translated menu label (use __( 'Literal', 'easylink-automations' ) at call site).
     */
    private static function submenu(string $name, string $hash): array
    {
        return [
            'parent'     => Config::SLUG,
            'type'       => 'submenu',
            'name'       => $name,
            'capability' => 'manage_options',
            'slug'       => Config::SLUG . $hash,
        ];
    }
}
