<?php
/**
 * One-time welcome banner shown in WP-admin after the plugin is
 * activated, until the user dismisses it. Tells them what the rail is
 * and how to start using it.
 */
if (!defined('ABSPATH')) exit;

class SailWP_Copilot_Activation_Notice {
    const DISMISS_OPTION = 'sailwp_copilot_welcome_dismissed';

    public function __construct() {
        add_action('admin_notices',   [$this, 'render']);
        add_action('admin_init',      [$this, 'maybe_dismiss']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_styles']);
    }

    public function maybe_dismiss() {
        if (!isset($_GET['swcp_dismiss_welcome'])) return;
        if (!current_user_can('edit_posts')) return;
        if (!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_text_field($_GET['_wpnonce']), 'swcp_dismiss_welcome')) return;
        update_user_meta(get_current_user_id(), self::DISMISS_OPTION, time());
        wp_safe_redirect(remove_query_arg(['swcp_dismiss_welcome', '_wpnonce']));
        exit;
    }

    public function should_show() {
        if (!current_user_can('edit_posts')) return false;
        // The dashboard has its own hero strip (class-dashboard-hero.php)
        // that covers the same ground in a stronger way — showing both
        // makes the dashboard feel cluttered.
        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
        if ($screen && $screen->id === 'dashboard') return false;
        $dismissed = (int) get_user_meta(get_current_user_id(), self::DISMISS_OPTION, true);
        return !$dismissed;
    }

    public function enqueue_styles($hook) {
        if (!$this->should_show()) return;
        wp_register_style('sailwp-copilot-welcome', false);
        wp_enqueue_style('sailwp-copilot-welcome');
        $css = '
            .swcp-welcome { padding: 18px 22px 16px !important; border-left: 4px solid #e8845c !important; background: #fbf6ee !important; border-radius: 4px; display: flex; gap: 16px; align-items: center; box-shadow: 0 1px 3px rgba(122, 81, 56, 0.08); }
            .swcp-welcome-mark { flex-shrink: 0; width: 44px; height: 44px; background: #fff; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; box-shadow: 0 1px 3px rgba(232, 132, 92, 0.18); }
            .swcp-welcome-mark img { width: 32px; height: 32px; display: block; }
            .swcp-welcome-body { flex: 1; }
            .swcp-welcome-body h2 { margin: 0 0 4px; font-size: 16px; font-weight: 600; color: #3a2e24; }
            .swcp-welcome-body p { margin: 0 0 10px; color: #6c5d52; line-height: 1.5; }
            .swcp-welcome-actions { display: flex; gap: 10px; margin-top: 4px; }
            .swcp-welcome-actions a.swcp-btn-primary { background: #e8845c; color: #fff !important; border: none; padding: 8px 14px; border-radius: 6px; text-decoration: none; font-size: 13px; font-weight: 500; }
            .swcp-welcome-actions a.swcp-btn-primary:hover { background: #d36e47; }
            .swcp-welcome-actions a.swcp-btn-link { color: #7d6e60 !important; text-decoration: none; padding: 8px 0; font-size: 13px; }
            .swcp-welcome-actions a.swcp-btn-link:hover { color: #3a2e24 !important; text-decoration: underline; }
            .swcp-welcome-fineprint { margin: 10px 0 0 !important; font-size: 12px; color: #9c8d80; }
            .swcp-welcome-fineprint a { color: #7d6e60; text-decoration: none; }
            .swcp-welcome-fineprint a:hover { color: #3a2e24; text-decoration: underline; }
        ';
        wp_add_inline_style('sailwp-copilot-welcome', $css);
    }

    public function render() {
        if (!$this->should_show()) return;
        $dismiss_url = wp_nonce_url(add_query_arg('swcp_dismiss_welcome', 1), 'swcp_dismiss_welcome');
        $icon_url    = esc_url(SAILWP_COPILOT_URL . 'assets/img/sailwp-icon.png');
        ?>
        <div class="notice swcp-welcome" role="status">
            <div class="swcp-welcome-mark">
                <img src="<?php echo $icon_url; ?>" alt="" />
            </div>
            <div class="swcp-welcome-body">
                <h2>🌿 <?php esc_html_e('SailWP Co-pilot is ready', 'sailwp-ai-copilot'); ?></h2>
                <p><strong><?php esc_html_e('First, open the page you want to work on.', 'sailwp-ai-copilot'); ?></strong> <?php esc_html_e('Then click the boat in the bottom-right corner. The Co-pilot edits whichever page you have open, so opening it from the Plugins screen will leave it without a page to design.', 'sailwp-ai-copilot'); ?></p>
                <p><?php esc_html_e('Once you are on a page, the Co-pilot can redesign it, rewrite it, swap images, tidy headings, and more. Just say what you want.', 'sailwp-ai-copilot'); ?></p>
                <div class="swcp-welcome-actions">
                    <a href="<?php echo esc_url(admin_url('edit.php?post_type=page')); ?>" class="swcp-btn-primary"><?php esc_html_e('Open my pages', 'sailwp-ai-copilot'); ?></a>
                    <a href="<?php echo esc_url($dismiss_url); ?>" class="swcp-btn-link"><?php esc_html_e('Dismiss', 'sailwp-ai-copilot'); ?></a>
                </div>
                <p class="swcp-welcome-fineprint">
                    <?php
                    printf(
                        /* translators: 1: Privacy link, 2: Terms link */
                        esc_html__('Powered by AI services at sailwp.com. %1$s · %2$s', 'sailwp-ai-copilot'),
                        '<a href="https://sailwp.com/legal/privacy" target="_blank" rel="noopener">' . esc_html__('Privacy', 'sailwp-ai-copilot') . '</a>',
                        '<a href="https://sailwp.com/legal/terms" target="_blank" rel="noopener">' . esc_html__('Terms', 'sailwp-ai-copilot') . '</a>'
                    );
                    ?>
                </p>
            </div>
        </div>
        <?php
    }
}
