<?php
/**
 * MindWebConcierge Plugin Deactivation Handlers
 *
 * @package MindWebConcierge
 */

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

function mindweco_plugin_cancel_on_deactivate() {
	$stripe_base_url = defined( 'MINDWECO_STRIPE_URL' ) ? MINDWECO_STRIPE_URL : '';
	$app_guid        = get_option( 'mindweco_plugin_guid' );

	if ( empty( $stripe_base_url ) || empty( $app_guid ) ) {
		update_option( 'mindweco_cancel_log', 'App GUID or Stripe URL missing on deactivate.' );
		return;
	}

	$active_subscription = mindweco_stripe_active_subscription( $stripe_base_url, $app_guid );

	if ( ! $active_subscription || empty( $active_subscription['stripeSubscriptionId'] ) ) {
		update_option( 'mindweco_cancel_log', 'No active subscription found on deactivate.' );
		return;
	}

	$subscription_id = sanitize_text_field( $active_subscription['stripeSubscriptionId'] );
	$response        = mindweco_plugin_cancel_subscription( $stripe_base_url, $app_guid, $subscription_id, true );

	if ( isset( $response['error'] ) ) {
		update_option( 'mindweco_cancel_log', 'Error cancelling subscription on deactivate: ' . sanitize_text_field( $response['error'] ) );
	} else {
		update_option( 'mindweco_cancel_log', 'Subscription cancelled successfully on deactivate. ID: ' . sanitize_text_field( $subscription_id ) );

		update_option( 'mindweco_cancel_response_debug', $response );

		if ( is_array( $response ) ) {
			$trial_response = get_option( 'mindweco_trial_response', array() );
			if ( is_array( $trial_response ) ) {
				$updated = array_merge( $trial_response, $response );
				update_option( 'mindweco_trial_response', $updated );
			}
		}

		if ( empty( $response ) || ! is_array( $response ) ) {
			$updated_subscription = mindweco_stripe_active_subscription( $stripe_base_url, $app_guid );
			if ( ! empty( $updated_subscription ) && is_array( $updated_subscription ) && ! isset( $updated_subscription['error'] ) ) {
				update_option( 'mindweco_trial_response', $updated_subscription );
			}
		}
	}
}

function mindweco_plugin_logout_knowledge_on_deactivate() {
	$logged_status = get_option( 'mindweco_logged_status' );

	if ( (int) $logged_status !== 1 ) {
		return;
	}

	$base_url = defined( 'MINDWECO_NOTEBOOKS_URL' ) ? MINDWECO_NOTEBOOKS_URL : '';

	$indexing_options = get_option( 'mindweco_plugin_indexing', array() );
	$external_app_id  = isset( $indexing_options['id'] ) ? $indexing_options['id'] : null;

	$knowledge_options = get_option( 'mindweco_knowledge_repository_options', array() );
	$knowledge_full    = isset( $knowledge_options['full_result'] ) ? $knowledge_options['full_result'] : array();
	$user_id           = isset( $knowledge_full['user']['id'] ) ? $knowledge_full['user']['id'] : null;

	$response = mindweco_plugin_knowledge_repository_logout( $base_url, $external_app_id, $user_id );

	update_option( 'mindweco_logged_status', 0 );

	$success = ( true === $response ) ||
	           ( is_array( $response ) && isset( $response['http_code'] ) && 200 === (int) $response['http_code'] );

	if ( $success ) {
		delete_option( 'mindweco_knowledge_repository_options' );
	}
}