<?php
/**
 * Campaign-Product Bridge
 *
 * Automatically creates and syncs WooCommerce donation products with campaigns.
 *
 * @package DonorCore\WooCommerce
 */

namespace DonorCore\WooCommerce;

defined( 'ABSPATH' ) || exit;

/**
 * Campaign Product Bridge Class
 */
class Campaign_Product_Bridge {

	/**
	 * Initialize hooks.
	 */
	public function init() {
		add_action( 'save_post_donorcore_campaign', array( $this, 'sync_product' ), 20, 3 );
		add_action( 'trashed_post', array( $this, 'trash_product' ) );
		add_action( 'untrashed_post', array( $this, 'untrash_product' ) );
		add_action( 'before_delete_post', array( $this, 'delete_product' ) );
	}

	/**
	 * Sync campaign with donation product.
	 *
	 * @param int      $post_id Post ID.
	 * @param \WP_Post $post    Post object.
	 * @param bool     $update  Whether this is an update.
	 */
	public function sync_product( $post_id, $post, $update ) {
		// Skip autosave and revisions.
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		if ( wp_is_post_revision( $post_id ) ) {
			return;
		}

		// Only sync published campaigns.
		if ( 'publish' !== $post->post_status ) {
			return;
		}

		// Check if product already exists.
		$product_id = get_post_meta( $post_id, '_donorcore_campaign_product_id', true );

		if ( $product_id && get_post( $product_id ) ) {
			// Update existing product.
			$this->update_product( $product_id, $post_id );
		} else {
			// Create new product.
			$product_id = $this->create_product( $post_id );
			
			if ( $product_id ) {
				update_post_meta( $post_id, '_donorcore_campaign_product_id', $product_id );
			}
		}
	}

	/**
	 * Create donation product for campaign.
	 *
	 * @param int $campaign_id Campaign post ID.
	 * @return int|false Product ID or false on failure.
	 */
	private function create_product( $campaign_id ) {
		$campaign = get_post( $campaign_id );
		
		if ( ! $campaign ) {
			return false;
		}

		// Create product.
		$product = new \WC_Product_Simple();
		$product->set_name( 'Donation: ' . $campaign->post_title );
		$product->set_slug( 'donation-' . $campaign->post_name );
		$product->set_status( 'publish' );
		$product->set_catalog_visibility( 'hidden' );
		// Don't sync description to avoid Gutenberg blocks appearing in cart
		$product->set_description( '' );
		$product->set_short_description( '' );
		
		// Set as virtual.
		$product->set_virtual( true );
		$product->set_sold_individually( false );
		
		// Save to get ID.
		$product_id = $product->save();

		if ( ! $product_id ) {
			return false;
		}

		// Set product type to donation.
		wp_set_object_terms( $product_id, 'donation', 'product_type' );

		// Copy campaign meta to product.
		$this->sync_meta( $campaign_id, $product_id );

		// Link back to campaign.
		update_post_meta( $product_id, '_donorcore_campaign_id', $campaign_id );

		return $product_id;
	}

	/**
	 * Update existing donation product.
	 *
	 * @param int $product_id  Product ID.
	 * @param int $campaign_id Campaign ID.
	 */
	private function update_product( $product_id, $campaign_id ) {
		$campaign = get_post( $campaign_id );
		$product  = wc_get_product( $product_id );

		if ( ! $campaign || ! $product ) {
			return;
		}

		// Update product details.
		$product->set_name( 'Donation: ' . $campaign->post_title );
		// Don't sync description to avoid Gutenberg blocks appearing in cart
		$product->set_description( '' );
		$product->set_short_description( '' );
		$product->save();

		// Ensure product type is donation (fixes issues if created before registration).
		wp_set_object_terms( $product_id, 'donation', 'product_type' );

		// Sync meta.
		$this->sync_meta( $campaign_id, $product_id );
	}

	/**
	 * Sync campaign meta to product meta.
	 *
	 * @param int $campaign_id Campaign ID.
	 * @param int $product_id  Product ID.
	 */
	private function sync_meta( $campaign_id, $product_id ) {
		// Minimum donation.
		$min_donation = floatval( get_post_meta( $campaign_id, '_donorcore_campaign_min_donation', true ) );
		update_post_meta( $product_id, '_donorcore_min_donation', $min_donation );

		// Suggested amounts.
		$suggested = sanitize_text_field( get_post_meta( $campaign_id, '_donorcore_campaign_suggested_amounts', true ) );
		update_post_meta( $product_id, '_donorcore_suggested_amounts', $suggested );

		// Campaign ID reference.
		update_post_meta( $product_id, '_donorcore_campaign_id', absint( $campaign_id ) );
	}

	/**
	 * Trash linked product when campaign is trashed.
	 *
	 * @param int $post_id Post ID.
	 */
	public function trash_product( $post_id ) {
		if ( 'donorcore_campaign' !== get_post_type( $post_id ) ) {
			return;
		}

		$product_id = get_post_meta( $post_id, '_donorcore_campaign_product_id', true );
		
		if ( $product_id ) {
			wp_trash_post( $product_id );
		}
	}

	/**
	 * Untrash linked product when campaign is restored.
	 *
	 * @param int $post_id Post ID.
	 */
	public function untrash_product( $post_id ) {
		if ( 'donorcore_campaign' !== get_post_type( $post_id ) ) {
			return;
		}

		$product_id = get_post_meta( $post_id, '_donorcore_campaign_product_id', true );
		
		if ( $product_id ) {
			wp_untrash_post( $product_id );
		}
	}

	/**
	 * Delete linked product when campaign is permanently deleted.
	 *
	 * @param int $post_id Post ID.
	 */
	public function delete_product( $post_id ) {
		if ( 'donorcore_campaign' !== get_post_type( $post_id ) ) {
			return;
		}

		$product_id = get_post_meta( $post_id, '_donorcore_campaign_product_id', true );
		
		if ( $product_id ) {
			wp_delete_post( $product_id, true );
		}
	}
}
