<?php
/**
 * Admin Manager Class
 *
 * Handles all admin-related functionality
 *
 * @package AIProductContentImageGenerator
 * @since 1.0.0
 */

namespace AIProductContentImageGenerator\Admin;

use AIProductContentImageGenerator\Core\Container;
use AIProductContentImageGenerator\Core\Router;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * AdminManager class for handling admin functionality
 */
class AdminManager {
    /**
     * Container instance
     *
     * @var Container
     */
    private $container;

    /**
     * Router instance
     *
     * @var Router
     */
    private $router;

    /**
     * Constructor
     *
     * @param Container $container
     * @param Router $router
     */
    public function __construct(Container $container, Router $router) {
        $this->container = $container;
        $this->router = $router;
    }

    /**
     * Initialize admin functionality
     */
    public function init() {
        add_action('admin_init', [$this, 'admin_init']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
        add_action('admin_notices', [$this, 'admin_notices']);
        
        $this->register_admin_routes();
        
        $settings_manager = new SettingsManager();
        $settings_manager->init();
    }

    /**
     * Admin init callback
     */
    public function admin_init() {
        if (!$this->is_woocommerce_active()) {
            add_action('admin_notices', [$this, 'woocommerce_missing_notice']);
            return;
        }

        $this->register_settings();
    }

    /**
     * Enqueue admin assets
     *
     * @param string $hook_suffix
     */
    public function enqueue_admin_assets($hook_suffix) {
        // Only load on plugin pages
        if (!$this->router->is_plugin_page()) {
            return;
        }

        $plugin_url = plugin_dir_url(dirname(dirname(__FILE__)));
        $plugin_version = '1.0.0';

        // Enqueue CSS
        wp_enqueue_style(
            'apcig-admin-style',
            $plugin_url . 'assets/css/admin.css',
            [],
            $plugin_version
        );

        // Enqueue JS
        wp_enqueue_script(
            'apcig-admin-script',
            $plugin_url . 'assets/js/admin.js',
            ['jquery', 'wp-api-fetch'],
            $plugin_version,
            true
        );

        // Localize script
        wp_localize_script('apcig-admin-script', 'apcig_admin', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('apcig_ajax_nonce'),
            'current_page' => $this->router->get_current_page(),
            'strings' => [
                'processing' => __('Processing...', 'contengen-ai-product-content-image-generator'),
                'error' => __('An error occurred', 'contengen-ai-product-content-image-generator'),
                'success' => __('Success!', 'contengen-ai-product-content-image-generator'),
                'generating' => __('Generating...', 'contengen-ai-product-content-image-generator'),
                'confirm_bulk' => __('Are you sure you want to process all selected products?', 'contengen-ai-product-content-image-generator')
            ]
        ]);
    }

    /**
     * Register admin routes
     */
    private function register_admin_routes() {
        $this->router->register_route('ai-product-content', [
            'title' => __('Content Generator', 'contengen-ai-product-content-image-generator'),
            'menu_title' => __('Content', 'contengen-ai-product-content-image-generator'),
            'callback' => [$this, 'render_content_page']
        ]);

        $this->router->register_route('ai-product-images', [
            'title' => __('Image Generator', 'contengen-ai-product-content-image-generator'),
            'menu_title' => __('Images', 'contengen-ai-product-content-image-generator'),
            'callback' => [$this, 'render_images_page']
        ]);

        $this->router->register_route('ai-product-bulk', [
            'title' => __('Bulk Processing', 'contengen-ai-product-content-image-generator'),
            'menu_title' => __('Bulk Process', 'contengen-ai-product-content-image-generator'),
            'callback' => [$this, 'render_bulk_page']
        ]);

        $this->router->register_route('ai-product-settings', [
            'title' => __('Settings', 'contengen-ai-product-content-image-generator'),
            'menu_title' => __('Settings', 'contengen-ai-product-content-image-generator'),
            'callback' => [$this, 'render_settings_page']
        ]);
    }

    /**
     * Register plugin settings
     */
    private function register_settings() {
        // Settings are now registered via SettingsManager class
        // This method is kept for backward compatibility but no longer registers settings
        // to avoid duplication with SettingsManager::register_settings()
    }

    /**
     * Render content generator page
     */
    public function render_content_page() {
        $this->render_page_with_layout('content-generator');
    }

    /**
     * Render image generator page
     */
    public function render_images_page() {
        $this->render_page_with_layout('image-generator');
    }

    /**
     * Render bulk processing page
     */
    public function render_bulk_page() {
        $this->render_page_with_layout('bulk-processing');
    }

    /**
     * Render settings page
     */
    public function render_settings_page() {
        $this->render_page_with_layout('settings');
    }

    /**
     * Display admin notices
     */
    public function admin_notices() {
    }

    /**
     * WooCommerce missing notice
     */
    public function woocommerce_missing_notice() {
        echo '<div class="notice notice-error"><p>';
        echo esc_html(__('AI Product Content & Image Generator requires WooCommerce to be installed and activated.', 'contengen-ai-product-content-image-generator'));
        echo '</p></div>';
    }

    /**
     * Check if WooCommerce is active
     *
     * @return bool
     */
    private function is_woocommerce_active() {
        return class_exists('WooCommerce');
    }

    /**
     * Render page with layout wrapper
     *
     * @param string $page_name
     */
    private function render_page_with_layout($page_name) {
        $layout_file = dirname(__FILE__) . '/Views/layout.php';
        $content_file = dirname(__FILE__) . '/Views/' . $page_name . '.php';
        
        if (!file_exists($layout_file)) {
            echo '<div class="notice notice-error"><p>Layout file not found.</p></div>';
            return;
        }
        
        if (!file_exists($content_file)) {
            echo '<div class="notice notice-error"><p>Content file not found: ' . esc_html($page_name) . '</p></div>';
            return;
        }
        
        $current_page = $page_name;
        $content_file_path = $content_file;
        
        include $layout_file;
    }
}