<?php

namespace NeurogenesisTheme\Controllers\Admin;

use NeurogenesisTheme\Helpers\OnboardingNotice;
use NeurogenesisTheme\Helpers\RatingNotice;
use NeurogenesisTheme\Helpers\RecommendedPlugin;
use NeurogenesisTheme\Traits\HasTemplate;

class AdminNotices
{
    use HasTemplate;

    public function __construct()
    {
        add_action('admin_notices', [__CLASS__, 'renderOnboardingNotice']);
        add_action('admin_notices', [__CLASS__, 'renderRatingNotice']);
    }

    /**
     * Activation / onboarding notice via the core admin_notices API.
     */
    public static function renderOnboardingNotice(): void
    {
        if (!OnboardingNotice::should_render()) {
            return;
        }

        $html = self::renderOnboarding();

        if ('' === $html) {
            return;
        }

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- template escapes its own output.
        echo $html;
    }

    public static function renderOnboarding(): string
    {
        $plugin_status  = RecommendedPlugin::get_status();
        $dashboard_url  = neurogenesis_theme_generate_page_url('dashboard');
        $active_cta_url = (string) apply_filters(
            'neurogenesis_theme_onboarding_import_url',
            $dashboard_url
        );

        $notice_args = [
            'cta_url'        => 'active' === $plugin_status ? $active_cta_url : $dashboard_url,
            'plugin_status'  => $plugin_status,
            'hero_image_url' => neurogenesis_theme_theme_url('assets/admin/images/onboarding-hero.png'),
        ];

        return (string) self::template('notices/onboarding', $notice_args, false);
    }

    /**
     * Rating notice via the core admin_notices API.
     */
    public static function renderRatingNotice(): void
    {
        if (!RatingNotice::should_render()) {
            return;
        }

        $html = self::renderRating();

        if ('' === $html) {
            return;
        }

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- template escapes its own output.
        echo $html;
    }

    public static function renderRating(): string
    {
        $notice_args = RatingNotice::view_context();

        return (string) self::template('notices/rating', $notice_args, false);
    }
}
