<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Whether the current screen belongs to Algo Forms.
 *
 * @param string|null $hook Current admin hook.
 * @return bool
 */
function algo_forms_is_admin_screen($hook = null)
{
    if ($hook === null && function_exists('get_current_screen')) {
        $screen = get_current_screen();
        $hook = $screen ? (string) $screen->id : '';
    }
    $hook = (string) $hook;
    return $hook !== '' && (
        strpos($hook, 'algo-forms') !== false ||
        strpos($hook, 'toplevel_page_algo-forms') !== false
    );
}

/**
 * Branded sidebar menu styles (all admin screens).
 *
 * @return void
 */
function algo_forms_enqueue_admin_menu_assets()
{
    $css = ALGO_FORMS_PLUGIN_DIR . 'assets/css/admin-menu.css';
    wp_enqueue_style(
        'algo-forms-admin-menu',
        ALGO_FORMS_PLUGIN_URL . 'assets/css/admin-menu.css',
        array(),
        file_exists($css) ? (string) filemtime($css) : ALGO_FORMS_VERSION
    );
}
add_action('admin_enqueue_scripts', 'algo_forms_enqueue_admin_menu_assets');

/**
 * @param string $hook
 * @return void
 */
function algo_forms_enqueue_admin_assets($hook)
{
    $hook = (string) $hook;
    $is_forms = algo_forms_is_admin_screen($hook);
    $is_plugins = $hook === 'plugins.php';

    if (!$is_forms && !$is_plugins) {
        return;
    }

    if ($is_forms) {
        $css = ALGO_FORMS_PLUGIN_DIR . 'assets/css/admin.css';
        wp_enqueue_style(
            'algo-forms-admin',
            ALGO_FORMS_PLUGIN_URL . 'assets/css/admin.css',
            array(),
            file_exists($css) ? (string) filemtime($css) : ALGO_FORMS_VERSION
        );
    }

    if ($is_plugins) {
        $mark = algo_forms_brand_asset_url('algo-forms-mark.svg');
        if ($mark === '') {
            return;
        }
        $css = '
			.plugins tr[data-plugin="algo-forms/algo-forms.php"] .plugin-title strong {
				display: inline-flex;
				align-items: center;
				gap: 0.55rem;
			}
			.plugins tr[data-plugin="algo-forms/algo-forms.php"] .plugin-title strong::before {
				content: "";
				display: inline-block;
				width: 28px;
				height: 28px;
				border-radius: 8px;
				background: #e8f4fc url(' . esc_url($mark) . ') center / cover no-repeat;
				box-shadow: 0 4px 10px rgba(111, 168, 200, 0.22);
				flex: 0 0 auto;
			}
		';
        wp_register_style('algo-forms-plugins-list', false, array(), ALGO_FORMS_VERSION);
        wp_enqueue_style('algo-forms-plugins-list');
        wp_add_inline_style('algo-forms-plugins-list', $css);
    }
}
add_action('admin_enqueue_scripts', 'algo_forms_enqueue_admin_assets');

/**
 * Inline SVG icon for admin UI.
 *
 * @param string               $name  Icon key.
 * @param array<string,string> $attrs Optional attributes.
 * @return string
 */
function algo_forms_admin_icon($name, $attrs = array())
{
    $paths = array(
        'dashboard' => '<path d="M4 4h7v7H4V4zm9 0h7v4h-7V4zM4 13h7v7H4v-7zm9-3h7v10h-7V10z"/>',
        'forms' => '<path d="M6 3h9l3 3v13a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2zm8 1.5V7h2.5L14 4.5zM8 11h8v1.5H8V11zm0 3.5h8V16H8v-1.5zM8 8h4v1.5H8V8z"/>',
        'inbox' => '<path d="M3 6.5A2.5 2.5 0 0 1 5.5 4h13A2.5 2.5 0 0 1 21 6.5v11A2.5 2.5 0 0 1 18.5 20h-13A2.5 2.5 0 0 1 3 17.5v-11zm2.2 8.5 2.7-3.2h2.1c.4 1.4 1.7 2.4 3.2 2.4s2.8-1 3.2-2.4h2.1l2.7 3.2H5.2zm13.3-5.2L16.8 6H7.2L5.5 9.8h3c.4-1.2 1.5-2 2.8-2s2.4.8 2.8 2h4.6z"/>',
        'smtp' => '<path d="M4 7.5A2.5 2.5 0 0 1 6.5 5h11A2.5 2.5 0 0 1 20 7.5v9A2.5 2.5 0 0 1 17.5 19h-11A2.5 2.5 0 0 1 4 16.5v-9zm2.2-.3 5.5 4.1c.2.15.5.15.7 0l5.5-4.1H6.2zm11.6 2.2-4.8 3.6c-.9.67-2.1.67-3 0L5.2 9.4v7.1c0 .17.13.3.3.3h13c.17 0 .3-.13.3-.3V9.4z"/>',
        'bell' => '<path d="M12 3a5.5 5.5 0 0 1 5.5 5.5v2.2c0 .7.2 1.4.6 2l1.2 1.7c.5.7 0 1.7-.9 1.7H5.6c-.9 0-1.4-1-.9-1.7l1.2-1.7c.4-.6.6-1.3.6-2V8.5A5.5 5.5 0 0 1 12 3zm-2.2 14.2a2.2 2.2 0 0 0 4.4 0h-4.4z"/>',
        'plus' => '<path d="M11 5h2v6h6v2h-6v6h-2v-6H5v-2h6V5z"/>',
        'check' => '<path d="M9.2 16.4 4.8 12l1.4-1.4 3 3 8-8.1L18.6 7l-9.4 9.4z"/>',
        'warning' => '<path d="M12 4.5 21 19H3L12 4.5zm0 4.2c-.5 0-.9.4-.9.9v3.4c0 .5.4.9.9.9s.9-.4.9-.9V9.6c0-.5-.4-.9-.9-.9zm0 7.1a1.1 1.1 0 1 0 0 2.2 1.1 1.1 0 0 0 0-2.2z"/>',
        'shortcode' => '<path d="M8.2 7.2 4.5 12l3.7 4.8-1.6 1.2L2 12l4.6-6 1.6 1.2zm7.6 0 1.6-1.2L22 12l-4.6 6-1.6-1.2L19.5 12l-3.7-4.8zM13.4 6l-2.7 12h-1.8l2.7-12h1.8z"/>',
        'clock' => '<path d="M12 3.5a8.5 8.5 0 1 1 0 17 8.5 8.5 0 0 1 0-17zm.8 3.7h-1.6v5.1l3.8 2.3.8-1.3-3-1.8V7.2z"/>',
        'users' => '<path d="M9 11a3.5 3.5 0 1 1 0-7 3.5 3.5 0 0 1 0 7zm6.5-.5a3 3 0 1 1 0-6 3 3 0 0 1 0 6zM3.5 19.5c0-2.8 2.5-5 5.5-5s5.5 2.2 5.5 5v.5h-11v-.5zm11.2-.5c0-1.5.6-2.8 1.6-3.8 1-.7 2.1-1.1 3.4-1.1 2.5 0 4.5 1.6 4.8 3.8v1.1h-9.8V19z"/>',
        'external' => '<path d="M14 5h5v5h-1.6V7.7l-7.2 7.2-1.1-1.1 7.2-7.2H14V5zM6.5 7H11v1.6H7.6v9.8h9.8V13H19v4.5A1.5 1.5 0 0 1 17.5 19h-11A1.5 1.5 0 0 1 5 17.5v-11A1.5 1.5 0 0 1 6.5 7z"/>',
    );

    $path = $paths[$name] ?? $paths['forms'];
    $class = isset($attrs['class']) ? ' ' . sanitize_html_class($attrs['class']) : '';
    return '<svg class="af-icon' . esc_attr($class) . '" viewBox="0 0 24 24" width="22" height="22" aria-hidden="true" focusable="false">' . $path . '</svg>';
}

/**
 * URL for a plugin brand asset under assets/images/.
 *
 * @param string $filename Filename.
 * @return string
 */
function algo_forms_brand_asset_url($filename)
{
    $filename = ltrim((string) $filename, '/');
    $path = ALGO_FORMS_PLUGIN_DIR . 'assets/images/' . $filename;
    if ($filename === '' || !is_readable($path)) {
        return '';
    }
    $ver = (string) filemtime($path);
    return ALGO_FORMS_PLUGIN_URL . 'assets/images/' . rawurlencode(basename($filename)) . '?ver=' . rawurlencode($ver);
}

/**
 * Render shared “Powered by Algorisys” strip.
 *
 * @return void
 */
function algo_forms_powered_by_html()
{
    $logo = algo_forms_brand_asset_url('algorisys-logo.webp');
    echo '<div class="af-powered">';
    echo '<span class="af-powered-label">' . esc_html__('Powered by', 'algo-forms') . '</span>';
    if ($logo !== '') {
        echo '<a class="af-powered-logo" href="https://algorisys.com/" target="_blank" rel="noopener noreferrer">';
        echo '<img src="' . esc_url($logo) . '" alt="' . esc_attr__('Algorisys', 'algo-forms') . '" width="120" height="33">';
        echo '</a>';
    } else {
        echo '<a class="af-powered-text" href="https://algorisys.com/" target="_blank" rel="noopener noreferrer">' . esc_html__('Algorisys', 'algo-forms') . '</a>';
    }
    echo '</div>';
}

/**
 * Open a shared Algo Forms admin shell.
 *
 * Hierarchy: product name (primary) → page title → supporting line → Powered by Algorisys.
 *
 * @param string $title Page title (e.g. Dashboard).
 * @param string $subtitle Optional subtitle.
 * @param string $tone   Header tone: mint|sky|peach|lavender|slate.
 * @param bool   $show_mascot Kept for callers; large mascot art is no longer shown.
 * @return void
 */
function algo_forms_admin_shell_start($title, $subtitle = '', $tone = 'sky', $show_mascot = false)
{
    unset($show_mascot);
    $tone = sanitize_key($tone);
    if (!in_array($tone, array('mint', 'sky', 'peach', 'lavender', 'slate'), true)) {
        $tone = 'sky';
    }
    $mark = algo_forms_brand_asset_url('algo-forms-mark.svg');

    echo '<div class="wrap algo-forms-admin af-tone-' . esc_attr($tone) . '">';
    echo '<div class="af-shell">';
    echo '<header class="af-hero">';
    echo '<div class="af-hero-main">';
    if ($mark !== '') {
        echo '<span class="af-logo" aria-hidden="true"><img src="' . esc_url($mark) . '" alt="" width="48" height="48"></span>';
    }
    echo '<div class="af-hero-copy">';
    echo '<h1 class="af-product-name">' . esc_html__('Algo Forms', 'algo-forms') . '</h1>';
    echo '<p class="af-page-title">' . esc_html($title) . '</p>';
    if ($subtitle !== '') {
        echo '<p class="af-hero-sub">' . esc_html($subtitle) . '</p>';
    }
    echo '</div>';
    echo '</div>';
    echo '<div class="af-hero-meta">';
    algo_forms_powered_by_html();
    echo '</div>';
    echo '</header>';
    echo '<div class="af-body">';
}

/**
 * @return void
 */
function algo_forms_admin_shell_end()
{
    echo '</div></div></div>';
}
