<?php
/**
 * Plugin Name: Churn Solution
 * Description: Integrates you site with Churn Solution, enabling cancellation flows that reduce churn and increase retention.
 * Version: 1.7.0
 * Requires at least: 5.6
 * Requires PHP: 5.6
 * Author: Churn Solution
 * Author URI: https://churnsolution.com
 * License: GPLv2
 * Text Domain: churnsolution
 * Domain Path: /languages
 */

if (!defined('ABSPATH')) {
    die("You cannot be here");
}


define( 'CHURNSOLUTION_BASE_FILE', __FILE__ );
define( 'CHURNSOLUTION_DIR', dirname( __FILE__ ) );
// Keep in sync with the plugin "Version" header above. Used as the version for
// bundled scripts/styles so every plugin upgrade busts the browser/CDN cache.
define( 'CHURNSOLUTION_VERSION', '1.7.0' );

// Includes
require_once( CHURNSOLUTION_DIR . '/includes/churnsolution-upgrade.php' );
require_once( CHURNSOLUTION_DIR . '/classes/class-churnsolution-connection.php' );
require_once CHURNSOLUTION_DIR . '/controllers/subscription-controller.php';
require_once CHURNSOLUTION_DIR . '/controllers/connection-controller.php';
require_once CHURNSOLUTION_DIR . '/classes/class-churnsolution-stripe-gateway.php';
require_once CHURNSOLUTION_DIR . '/includes/churnsolution-wall.php';
require_once CHURNSOLUTION_DIR . '/includes/churnsolution-texts.php';

// Load translations so the default texts can be translated with the usual
// WordPress tooling. Merchant overrides still win over any translation.
add_action( 'init', 'churnsolution_load_textdomain' );
function churnsolution_load_textdomain() {
    load_plugin_textdomain( 'churnsolution', false, dirname( plugin_basename( CHURNSOLUTION_BASE_FILE ) ) . '/languages' );
}


// Initialize database tables
global $wpdb;
$wpdb->churnsolution_connection = $wpdb->prefix . 'churnsolution_connection';

// Handle database setup/updates
if ( is_admin() || defined('WP_CLI') ) {
    churnsolution_handle_upgrade();
}

// Admin page
function churnsolution_add_pages(){
    add_menu_page('Churnsolution', 'Churnsolution', 'manage_options', 'churnsolution-dashboard', 'churnsolution_dashboard_page', '', 31);
}

function churnsolution_dashboard_page(){
    require_once CHURNSOLUTION_DIR . '/adminpages/dashboard.php';
}

add_action( 'admin_menu', 'churnsolution_add_pages' );

function churnsolution_add_asyncdefer_attribute($tag, $handle) {
    // if the unique handle/name of the registered script has 'async' in it
    if (strpos($handle, 'async') !== false) {
        // return the tag with the async attribute
        return str_replace( '<script ', '<script async ', $tag );
    }
    // if the unique handle/name of the registered script has 'defer' in it
    else if (strpos($handle, 'defer') !== false) {
        // return the tag with the defer attribute
        return str_replace( '<script ', '<script defer ', $tag );
    }
    // otherwise skip
    else {
        return $tag;
    }
}

add_filter('script_loader_tag', 'churnsolution_add_asyncdefer_attribute', 10, 2);

// Enqueue scripts
function churnsolution_enqueue_scripts() {
    wp_enqueue_script('churn-solution-custom-script', plugin_dir_url(__FILE__) . 'js/churn-solution-custom.js', array('jquery'), CHURNSOLUTION_VERSION, true);

    // Enqueue script for Churn Solution SDK
    wp_enqueue_script('churn-solution-sdk', 'https://app.churnsolution.com/sdk/index.min.js', array(), time(), true);

    // Pass AJAX URL and plugin options to JavaScript
    $plugin_options = array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('wp_rest'),
        'pmpro_cancel_nonce' => wp_create_nonce('pmpro_cancel-nonce'),
        'base_url' => esc_url(home_url()),
        'texts' => churnsolution_get_texts()
    );

    wp_localize_script('churn-solution-custom-script', 'churn_ajax_obj', $plugin_options);

    wp_enqueue_style( 'churn-solution-style',  plugins_url( 'css/style.css', CHURNSOLUTION_BASE_FILE ), array(), CHURNSOLUTION_VERSION );

}

// AJAX handler for get secret keys
add_action('wp_ajax_churnsolution_get_secret_keys', 'churnsolution_get_secret_keys');
function churnsolution_get_secret_keys() {
    // Check nonce for security
    check_ajax_referer('wp_rest', 'nonce');

    // The nonce above proves the request was not forged, not that its sender is entitled to this:
    // churnsolution_enqueue_scripts hands the same wp_rest nonce to every logged-in front-end visitor.
    if (!current_user_can('manage_options')) {
        wp_send_json_error(array('error' => 'Forbidden'), 403);
    }

    $secretData = Churnsolution_Stripe_Gateway::pmpro_get_secrets();

    wp_send_json_success($secretData);

    // Always exit to avoid further execution
    wp_die();
}

// Register scripts, styles
// add_action('init', function(){
add_action('wp_enqueue_scripts', 'churnsolution_enqueue_scripts');
// });

function churnsolution_enqueue_admin_scripts() {
    wp_register_script('churn-solution-koalendar-async', 'https://koalendar.com/assets/widget.js', array(), '1.2.2', true);
    wp_enqueue_script('churn-solution-koalendar-async');

    wp_register_script('churn-solution-dashboard-script', plugin_dir_url(__FILE__) . 'js/dashboard.js', array('jquery'), CHURNSOLUTION_VERSION, true);
    wp_enqueue_script('churn-solution-dashboard-script');

    $plugin_options = array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('wp_rest'),
        'churnsolution_base_url' => 'https://app.churnsolution.com/api/users',
        'churnsolutiontest_base_url' => 'https://app.churnsolutiontest.com/api/users',
        'store_url' => esc_url(home_url())
    );

    wp_localize_script('churn-solution-dashboard-script', 'churn_ajax_obj', $plugin_options);

    wp_enqueue_style( 'churn-solution-admin-style',  plugins_url( 'css/dashboard-style.css', CHURNSOLUTION_BASE_FILE ), array(), CHURNSOLUTION_VERSION );
}


add_action('admin_init', function(){
    add_action( 'admin_enqueue_scripts', 'churnsolution_enqueue_admin_scripts' );
});

// AJAX handler for calculating SHA256 HMAC hash
add_action('wp_ajax_churnsolution_get_hmac_hash', 'churnsolution_get_hmac_hash');
function churnsolution_get_hmac_hash() {
    // Check nonce for security
    check_ajax_referer('wp_rest', 'nonce');

    $subscriptionId = sanitize_text_field($_POST['subscription_id'] ?? '');

    if (empty($subscriptionId)) {
        wp_send_json_error(array('error' => 'subscription_id is required'), 400);
    }

    // The signature is what tells Churn Solution this launch is genuine, so signing whatever id was
    // asked for would let any customer act on any subscription on the store.
    if (!Churnsolution_Subscription_Controller::user_owns_subscription($subscriptionId)) {
        wp_send_json_error(array('error' => 'Unauthorized'), 403);
    }

    $connection = Churnsolution_Connection::get_connection();

    if (empty($connection) || empty($connection->getApiKey())) {
        wp_send_json_error(array('error' => 'Store is not connected'), 409);
    }

    // Calculate SHA256 HMAC hash
    $hash = hash_hmac('sha256', $subscriptionId, $connection->getApiKey(), true);
    $hmac_hash = bin2hex($hash);

    // Return the HMAC hash
    echo esc_attr($hmac_hash);

    // Always exit to avoid further execution
    wp_die();
}

add_action('plugins_loaded', 'churnsolution_tracking_init');
function churnsolution_tracking_init()
{

    if (!class_exists('WooCommerce')) {
        return;
    }

    require_once CHURNSOLUTION_DIR . '/includes/woocommerce-checkout-tracking-params.php';
    require_once CHURNSOLUTION_DIR . '/includes/churnsolution-webhook-health.php';
}

// Exception handler
function churnsolution_custom_handle_exception($exception) {

    $errorCode = $exception->getCode();
    $errorMessage = $exception->getMessage();

    if ($errorCode >= 400 && $errorCode < 600) {
        http_response_code($errorCode);
    } else {
        http_response_code(500); // Default to 500 Internal Server Error
    }

    // Return a JSON response
    wp_send_json([
        "error" => $errorMessage,
        "code" => $errorCode
    ], http_response_code());
}

set_exception_handler('churnsolution_custom_handle_exception');