<?php
/**
 * Plugin Name:       Formulate Pricing
 * Description:       Multi-Step Quotes & Checkout — build pricing and quote forms with card-choice fields, dynamic pricing, and WooCommerce checkout. Collect entries, track payments, and send notifications from a drag-and-drop builder.
 * Version:           2.0.0
 * Requires at least: 6.2
 * Tested up to:      7.0
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            Daniyal Hassan
 * Author URI:        https://profiles.wordpress.org/daniyaldotdev/
 * Text Domain:       formulate-pricing
 * Domain Path:       /languages
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 *
 * WC requires at least: 7.0
 * WC tested up to: 9.4
 */

defined( 'ABSPATH' ) || exit;

define( 'FORMULATE_PRICING_VERSION', '2.0.0' );
define( 'FORMULATE_PRICING_FILE',    __FILE__ );
define( 'FORMULATE_PRICING_PATH',    plugin_dir_path( __FILE__ ) );
define( 'FORMULATE_PRICING_URL',     plugin_dir_url( __FILE__ ) );
define( 'FORMULATE_PRICING_DB_VERSION', '1.2' );

require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-dependencies.php';

register_activation_hook( __FILE__, 'formulate_pricing_activate' );
register_deactivation_hook( __FILE__, 'formulate_pricing_deactivate' );

/**
 * Activation — requires WooCommerce.
 */
function formulate_pricing_activate() {
	Formulate_Pricing_Dependencies::on_activation();

	require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-database.php';

	Formulate_Pricing_Database::install();
	formulate_pricing_create_product();
}

/**
 * Deactivation.
 */
function formulate_pricing_deactivate() {
	flush_rewrite_rules();
}

/**
 * Load plugin only when WooCommerce is active.
 */
add_action( 'plugins_loaded', 'formulate_pricing_bootstrap', 20 );

function formulate_pricing_bootstrap() {
	if ( ! Formulate_Pricing_Dependencies::is_woocommerce_active() ) {
		add_action( 'admin_notices', array( 'Formulate_Pricing_Dependencies', 'admin_notice_missing_woocommerce' ) );
		return;
	}

	formulate_pricing_load();
}

/**
 * Bootstrap all plugin modules.
 */
function formulate_pricing_load() {
	require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-database.php';
	require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-form.php';
	require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-entry.php';
	require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-renderer.php';
	require_once FORMULATE_PRICING_PATH . 'includes/class-formulate-pricing-ajax.php';

	require_once FORMULATE_PRICING_PATH . 'includes/helpers-pricing.php';
	require_once FORMULATE_PRICING_PATH . 'admin/admin-menu.php';
	require_once FORMULATE_PRICING_PATH . 'includes/wc-integration.php';

	Formulate_Pricing_Ajax::init();

	add_shortcode( 'formulate_pricing_form', 'formulate_pricing_shortcode' );

	if ( get_option( 'formulate_pricing_db_version' ) !== FORMULATE_PRICING_DB_VERSION ) {
		Formulate_Pricing_Database::install();
	}
}

/**
 * Dynamic form shortcode.
 *
 * @param array $atts Shortcode attributes.
 * @return string
 */
function formulate_pricing_shortcode( $atts ) {
	$atts = shortcode_atts( array( 'id' => 0 ), $atts, 'formulate_pricing_form' );
	$id   = intval( $atts['id'] );
	if ( ! $id ) {
		return '<!-- formulate_pricing_form: missing id -->';
	}
	return Formulate_Pricing_Renderer::render( $id );
}

/**
 * Create or reuse the hidden WooCommerce product used when a form has no linked product.
 */
function formulate_pricing_create_product() {
	$existing_id = (int) get_option( 'formulate_pricing_product_id', 0 );
	if ( $existing_id > 0 && get_post( $existing_id ) ) {
		return;
	}

	$product_id = wp_insert_post(
		array(
			'post_title'  => __( 'Formulate Pricing Form Service', 'formulate-pricing' ),
			'post_status' => 'publish',
			'post_type'   => 'product',
		)
	);

	if ( is_wp_error( $product_id ) || ! $product_id ) {
		return;
	}

	wp_set_object_terms( $product_id, 'simple', 'product_type' );
	update_post_meta( $product_id, '_regular_price', '0' );
	update_post_meta( $product_id, '_price', '0' );
	update_post_meta( $product_id, '_virtual', 'yes' );
	update_post_meta( $product_id, '_sold_individually', 'yes' );
	update_post_meta( $product_id, '_manage_stock', 'no' );
	update_post_meta( $product_id, '_stock_status', 'instock' );
	update_post_meta( $product_id, '_visibility', 'hidden' );
	update_post_meta( $product_id, '_catalog_visibility', 'hidden' );

	update_option( 'formulate_pricing_product_id', (int) $product_id );
}
