<?php
/**
 * Custom Donation Product Type
 *
 * Extends WooCommerce product to support variable pricing (Name Your Price).
 *
 * @package DonorCore\WooCommerce
 */

namespace DonorCore\WooCommerce;

defined( 'ABSPATH' ) || exit;

/**
 * WC_Product_Donation Class
 */
class WC_Product_Donation extends \WC_Product_Simple {

	/**
	 * Initialize donation product.
	 *
	 * @param mixed $product Product data.
	 */
	public function __construct( $product = 0 ) {
		$this->supports[] = 'ajax_add_to_cart';
		
		parent::__construct( $product );

		// Set product as virtual (no shipping needed).
		$this->set_virtual( true );
		$this->set_downloadable( false );
		
		// Donations are tax-exempt.
		$this->set_tax_status( 'none' );
		$this->set_tax_class( '' );
		
		// Disable inventory management.
		$this->set_manage_stock( false );
		$this->set_stock_status( 'instock' );
	}

	/**
	 * Get product type.
	 *
	 * @return string
	 */
	public function get_type() {
		return 'donation';
	}

	/**
	 * Override price - donations are variable.
	 *
	 * @param string $context View or edit context.
	 * @return string
	 */
	public function get_price( $context = 'view' ) {
		// Check if price has been set dynamically (e.g., in cart)
		$price = parent::get_price( $context );
		
		// If a price has been set, use it; otherwise default to 0
		return $price ? $price : '0';
	}

	/**
	 * Get price HTML - custom display for donations.
	 *
	 * @return string
	 */
	public function get_price_html( $price = '' ) {
		$min_amount = $this->get_meta( '_donorcore_min_donation', true );
		
		if ( $min_amount && $min_amount > 0 ) {
			return sprintf(
				/* translators: %s: minimum donation amount */
				__( 'Minimum: %s', 'donorcore-donations' ),
				wc_price( $min_amount )
			);
		}

		return __( 'Name Your Price', 'donorcore-donations' );
	}

	/**
	 * Check if product is purchasable.
	 *
	 * @return bool
	 */
	public function is_purchasable() {
		return true;
	}

	/**
	 * Donation products are always in stock.
	 *
	 * @param string $context View or edit context.
	 * @return bool
	 */
	public function is_in_stock( $context = 'view' ) {
		return true;
	}

	/**
	 * Donation products are always available.
	 *
	 * @return bool
	 */
	public function is_on_sale( $context = 'view' ) {
		return false;
	}

	/**
	 * Get suggested amounts for this donation.
	 *
	 * @return array
	 */
	public function get_suggested_amounts() {
		$amounts = $this->get_meta( '_donorcore_suggested_amounts', true );

		if ( is_string( $amounts ) && ! empty( $amounts ) ) {
			$decoded = json_decode( $amounts, true );
			if ( is_array( $decoded ) ) {
				$amounts = $decoded;
			}
		}

		if ( is_array( $amounts ) ) {
			$amounts = array_filter( array_map( 'floatval', $amounts ) );
		}

		if ( empty( $amounts ) || ! is_array( $amounts ) ) {
			$global_suggested = get_option( 'donorcore_suggested_amounts', '50,100,250,500' );
			$amounts          = array_map( 'floatval', array_filter( array_map( 'trim', explode( ',', $global_suggested ) ) ) );
			if ( empty( $amounts ) ) {
				$amounts = array( 50, 100, 250, 500 );
			}
		}

		return array_values( $amounts );
	}

	/**
	 * Get minimum donation amount.
	 *
	 * @return float
	 */
	public function get_min_donation() {
		$min = $this->get_meta( '_donorcore_min_donation', true );
		return $min ? floatval( $min ) : 0;
	}

	/**
	 * Get linked campaign ID.
	 *
	 * @return int
	 */
	public function get_campaign_id() {
		$campaign_id = $this->get_meta( '_donorcore_campaign_id', true );
		return $campaign_id ? intval( $campaign_id ) : 0;
	}

	/**
	 * Donations never need shipping.
	 *
	 * @return bool
	 */
	public function needs_shipping() {
		return false;
	}

	/**
	 * Donations are tax-exempt.
	 *
	 * @param string $context View or edit context.
	 * @return bool
	 */
	public function is_taxable( $context = 'view' ) {
		return false;
	}

	/**
	 * Get tax status - donations are always tax-exempt.
	 *
	 * @param string $context View or edit context.
	 * @return string
	 */
	public function get_tax_status( $context = 'view' ) {
		return 'none';
	}

	/**
	 * Donations have no tax class.
	 *
	 * @param string $context View or edit context.
	 * @return string
	 */
	public function get_tax_class( $context = 'view' ) {
		return '';
	}
}
