<?php

namespace AIPT\Admin\Notices;

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

/**
 * Migration and version upgrade notices.
 *
 * WordPress.org Guidelines 10/11 Compliant:
 * - Feature/upgrade notices shown only on plugin pages
 * - All notices are dismissible
 *
 * @package AIPT\Admin\Notices
 * @since   2.1.3
 */
class MigrationNotices {

    /**
     * Singleton instance.
     *
     * @var MigrationNotices|null
     */
    private static $instance = null;

    /**
     * Get singleton instance.
     *
     * @return MigrationNotices
     */
    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor - register notice hooks.
     */
    private function __construct() {
        add_action( 'admin_notices', array( $this, 'show_admin_notices' ) );
    }

    /**
     * Check if current admin page is a plugin page.
     *
     * @return bool True if on plugin page.
     */
    private function is_plugin_page(): bool {

        $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';

        if ( empty( $page ) ) {
            return false;
        }

        if ( 'ai-product-tools' === $page ) {
            return true;
        }

        if ( strpos( $page, 'aipt-' ) === 0 ) {
            return true;
        }

        return false;
    }
    
    /**
     * Sürüm güncelleme bildirimlerini oluşturur
     * 
     * @param string $current_version Mevcut eklenti sürümü
     * @param bool $is_first_activation İlk aktivasyon mu?
     * @return void
     */
    public function create_version_notices($current_version, $is_first_activation) {

    }
    
    /**
     * Bildirim oluşturma yardımcı metodu
     * 
     * @param string $key Bildirim anahtarı
     * @param string $message Bildirim mesajı
     * @param int $expiration Sona erme süresi (saniye)
     * @return void
     */
    public function create_notice($key, $message, $expiration = DAY_IN_SECONDS) {
        set_transient($key, $message, $expiration);
    }
    
    /**
     * Show admin notices - ONLY on plugin pages (Guideline 10/11 compliant).
     *
     * @return void
     */
    public function show_admin_notices() {

        if ( ! $this->is_plugin_page() ) {
            return;
        }

        $is_new_user      = get_option( 'aipt_first_activation', false ) === true;
        $setup_completed  = get_option( 'aipt_setup_completed', false );

        $version_upgrade_notice = get_transient( 'aipt_version_upgrade_notice' );
        if ( $version_upgrade_notice && ! $is_new_user && $setup_completed ) {
            ?>
            <div class="notice aipt-admin-notice is-dismissible" style="padding: 20px; border-left-color: #FF2E63; background: linear-gradient(to right, #1a1a1d, #2a2a2d); box-shadow: 0 1px 3px rgba(0,0,0,0.2);">
                <h3 style="margin-top: 0; color: #FF2E63; font-weight: 600;"><?php echo esc_html__( 'AI Product Tools - New Features Available!', 'ai-product-tools' ); ?></h3>
                <p style="font-size: 14px; margin-bottom: 15px; color: #f8f9fa;"><?php echo esc_html( $version_upgrade_notice ); ?></p>
                <div style="display: flex; gap: 10px;">
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=ai-product-tools' ) ); ?>" class="button" style="background: #FF2E63; border-color: #FF2E63; color: #f8f9fa; font-weight: 500; text-shadow: none; box-shadow: 0 2px 5px rgba(248, 56, 56, 0.2);">
                        <?php echo esc_html__( 'Go to Magic', 'ai-product-tools' ); ?> <span class="dashicons dashicons-arrow-right-alt" style="margin-top: 3px;"></span>
                    </a>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=aipt-settings' ) ); ?>" class="button" style="background: #2a2a2d; border-color: #1a1a1c; color: #f8f9fa; font-weight: 500; text-shadow: none;">
                        <?php echo esc_html__( 'Go to Settings', 'ai-product-tools' ); ?>
                    </a>
                </div>
            </div>
            <?php
            delete_transient( 'aipt_version_upgrade_notice' );
        }

        $bulk_feature_notice = get_transient( 'aipt_bulk_feature_notice' );
        if ( $bulk_feature_notice && ! $is_new_user && $setup_completed ) {
            ?>
            <div class="notice notice-info is-dismissible">
                <h3><?php echo esc_html__( 'AI Product Tools - Bulk Generator', 'ai-product-tools' ); ?></h3>
                <p><?php echo esc_html( $bulk_feature_notice ); ?></p>
                <p>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=ai-product-tools' ) ); ?>" class="button button-primary">
                        <?php echo esc_html__( 'Try Now', 'ai-product-tools' ); ?>
                        <span class="dashicons dashicons-arrow-right-alt2"></span>
                    </a>
                </p>
            </div>
            <?php
            delete_transient( 'aipt_bulk_feature_notice' );
        }
    }
}