<?php
/**
 * Agent Takeover (Premium Module)
 *
 * @package Replyan_AI
 */

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

final class Replyan_AI_Agent_Takeover {
	public function register_hooks(): void {
		add_action( 'wp_ajax_replyan_ai_toggle_manual_mode', array( $this, 'toggle_manual_mode' ) );
		add_action( 'wp_ajax_replyan_ai_admin_send_message', array( $this, 'admin_send_message' ) );
		add_action( 'wp_ajax_replyan_ai_admin_upload_image', array( $this, 'admin_upload_image' ) );
		add_filter( 'replyan_ai_intercept_message', array( $this, 'intercept_message' ), 10, 2 );
	}

	/**
	 * Intercept the frontend AI message if manual mode is enabled.
	 *
	 * @param bool   $should_intercept True to intercept and skip OpenAI.
	 * @param string $session_id       The current session ID.
	 * @return bool
	 */
	public function intercept_message( bool $should_intercept, string $session_id ): bool {
		$session   = Replyan_AI_DB::get_session_by_id( $session_id );
		$is_manual = $session && (int) $session->is_manual_mode === 1;

		if ( $is_manual ) {
			return true;
		}

		return $should_intercept;
	}

	/**
	 * Toggle manual mode via AJAX.
	 */
	public function toggle_manual_mode(): void {
		check_ajax_referer( 'replyan_ai_admin_messages', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => 'Unauthorized' ) );
		}

		$session_id = sanitize_text_field( wp_unslash( $_POST['session_id'] ?? '' ) );
		$is_manual  = rest_sanitize_boolean( wp_unslash( $_POST['is_manual'] ?? false ) );

		if ( ! $session_id ) {
			wp_send_json_error( array( 'message' => 'Missing session ID' ) );
		}

		Replyan_AI_DB::set_manual_mode( $session_id, $is_manual );
		wp_send_json_success();
	}

	/**
	 * Admin send message via AJAX.
	 */
	public function admin_send_message(): void {
		check_ajax_referer( 'replyan_ai_admin_messages', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => 'Unauthorized' ) );
		}

		$session_id = sanitize_text_field( wp_unslash( $_POST['session_id'] ?? '' ) );
		$content    = sanitize_textarea_field( wp_unslash( $_POST['message'] ?? '' ) );

		if ( ! $session_id || ! $content ) {
			wp_send_json_error( array( 'message' => 'Missing data' ) );
		}

		// Save the message
		Replyan_AI_DB::save_message( $session_id, 'assistant', $content );

		// Increment unread count for user so they see the badge
		Replyan_AI_DB::increment_unread( $session_id, 'user' );

		wp_send_json_success();
	}

	public function admin_upload_image(): void {
		check_ajax_referer( 'replyan_ai_admin_messages', 'nonce' );

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => 'Unauthorized' ) );
		}

		if ( empty( $_FILES['image'] ) ) {
			wp_send_json_error( array( 'message' => 'No file uploaded' ) );
		}

		$upload_url = Replyan_AI_Upload::handle_image_upload( $_FILES['image'] );

		if ( is_wp_error( $upload_url ) ) {
			wp_send_json_error( array( 'message' => $upload_url->get_error_message() ) );
		}

		wp_send_json_success( array( 'url' => $upload_url ) );
	}
}
