<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Admin;

use APD\Support\Capabilities;
use APD\Support\RequestData;

defined('ABSPATH') || exit;

class PluginLifecycle
{
    public function actionLinks(array $links) : array
    {
        $pluginLinks = [
            sprintf(
                '<a href="%s">%s</a>',
                esc_url(admin_url('admin.php?page=apd-dashboard')),
                esc_html__('Dashboard', 'asuka-product-designer')
            ),
            sprintf(
                '<a href="%s">%s</a>',
                esc_url(admin_url('admin.php?page=apd-settings')),
                esc_html__('Settings', 'asuka-product-designer')
            ),
            sprintf(
                '<a href="%s">%s</a>',
                esc_url(admin_url('admin.php?page=apd-support')),
                esc_html__('Support', 'asuka-product-designer')
            ),
        ];

        return array_merge($pluginLinks, $links);
    }

    public function rowMeta(array $links, string $file) : array
    {
        if (plugin_basename(ASUKA_PRODUCT_DESIGNER_FILE) !== $file) {
            return $links;
        }

        $links[] = sprintf(
            '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
            esc_url('https://portal.asukadev.com/wordpress-woocommerce'),
            esc_html__('View details', 'asuka-product-designer')
        );
        $links[] = sprintf(
            '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
            esc_url('https://portal.asukadev.com/documentation/wordpress'),
            esc_html__('Documentation', 'asuka-product-designer')
        );

        return $links;
    }

    public function maybeRedirectAfterActivation() : void
    {
        if (! get_transient('apd_activation_redirect')) {
            return;
        }

        delete_transient('apd_activation_redirect');

        $query = RequestData::query();
        if (
            ! current_user_can(Capabilities::MANAGE)
            || wp_doing_ajax()
            || wp_doing_cron()
            || is_network_admin()
            || isset($query['activate-multi'])
            || (defined('WP_CLI') && WP_CLI)
        ) {
            return;
        }

        wp_safe_redirect(admin_url('admin.php?page=apd-dashboard&apd_welcome=1'));
        exit;
    }
}
