<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class SBPAI_Core {
    public function init() {
        $this->load_textdomain();

        if ( is_admin() ) {
            $admin = new SBPAI_Admin();
            add_action( 'admin_menu', array( $admin, 'add_plugin_admin_menu' ) );
            add_action( 'admin_enqueue_scripts', array( $admin, 'enqueue_styles_scripts' ) );
            add_action( 'admin_post_sbpai_delete_all', array( $admin, 'handle_delete_all' ) );
        }

        $ajax_handler = new SBPAI_Ajax_Handler();
        add_action('wp_ajax_sbpai_queue_generation',      array($ajax_handler, 'handle_queue_generation'));
        add_action('wp_ajax_sbpai_get_polling_status',    array($ajax_handler, 'handle_polling_status'));
        add_action('wp_ajax_sbpai_save_settings',         array($ajax_handler, 'handle_save_settings'));
        add_action('wp_ajax_sbpai_quick_config_wc',       array($ajax_handler, 'handle_quick_config_wc'));
        add_action('wp_ajax_sbpai_reset_config_wc',       array($ajax_handler, 'handle_reset_config_wc'));
        add_action('wp_ajax_sbpai_save_ai_key',           array($ajax_handler, 'handle_save_ai_key'));
        add_action('wp_ajax_sbpai_test_ai_connection',    array($ajax_handler, 'handle_test_ai_connection'));
        add_action('wp_ajax_sbpai_set_active_provider',   array($ajax_handler, 'handle_set_active_provider'));
        add_action('wp_ajax_sbpai_save_primary_provider', array($ajax_handler, 'handle_save_primary_provider'));
        add_action('wp_ajax_sbpai_save_fallback_provider',array($ajax_handler, 'handle_save_fallback_provider'));
        add_action('wp_ajax_sbpai_ajax_delete_all',       array(new SBPAI_Admin(), 'handle_ajax_delete_all'));

        $scheduler = new SBPAI_Action_Scheduler();
        $scheduler->init();

        add_filter( 'get_avatar', array( $this, 'inject_dummy_avatars' ), 10, 5 );
    }

    public function load_textdomain() {
        load_plugin_textdomain(
            'sandboxpress-ai',
            false,
            dirname( plugin_basename( SBPAI_PLUGIN_DIR . 'sandboxpress-ai.php' ) ) . '/languages'
        );
    }

    public function inject_dummy_avatars( $avatar, $id_or_email, $size, $default, $alt ) {
        $user_id = false;
        if ( is_numeric( $id_or_email ) ) {
            $user_id = (int) $id_or_email;
        } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) ) {
            $user_id = (int) $id_or_email->user_id;
        } elseif ( $id_or_email instanceof WP_User ) {
            $user_id = $id_or_email->ID;
        }

        if ( $user_id ) {
            $avatar_id = get_user_meta( $user_id, '_sbpai_avatar_id', true );
            if ( $avatar_id ) {
                $img_url = wp_get_attachment_image_url( $avatar_id, array( $size, $size ) );
                if ( $img_url ) {
                    return "<img alt='" . esc_attr( $alt ) . "' src='" . esc_url( $img_url ) . "' class='avatar avatar-" . intval( $size ) . " photo' height='" . intval( $size ) . "' width='" . intval( $size ) . "' style='border-radius:50%; object-fit:cover;' />";
                }
            }
        }
        return $avatar;
    }
}
