<?php

class Wiaas_Package_CL_Pricing_Test extends Wiaas_Unit_Test_Case {
	var $shop_owner_id, $customer_id, $package_id, $cl_extras;

	function setUp() {
		parent::setUp();

		$this->shop_owner_id = wp_insert_term(
			'Shop owner organization',
			Wiaas_User_Organization::TAXONOMY_NAME)['term_id'];

		$this->customer_id = wp_insert_term(
			'Customer Organization',
			Wiaas_User_Organization::TAXONOMY_NAME)['term_id'];

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $this->create_new_product()));

		$this->package_id = $package->get_id();

		$this->cl_extras = array(
			'purchase_default' => array(
				'fixed' => 10,
				'recurrent' => 10,
				'services' => 10,
				'visible' => true
			),
			'purchase_24_default' => array(
				'fixed' => 10,
				'recurrent' => 10,
				'services' => 10,
				'visible' => true
			),
			'managed_36_default' => array(
				'fixed' => 10,
				'recurrent' => 10,
				'services' => 10,
				'visible' => true
			)
		);
	}

	/**
	 * @covers Wiaas_Package_CL_Pricing::set_extras()
	 */
	function test_packages_extras_set() {
		Wiaas_Package_CL_Pricing::set_extras(
			$this->shop_owner_id,
			$this->package_id,
			$this->cl_extras);

		// validate data saved
		$cl_extras = get_term_meta(
			$this->shop_owner_id,
			'_wiaas_cm_extras_' . $this->package_id,
			true);

		$this->_validate_default_cl_extras($cl_extras);
	}

	/**
	 * @covers Wiaas_Package_CL_Pricing::get_extras()
	 */
	function test_package_extras_retrieved() {
		update_term_meta(
			$this->shop_owner_id,
			'_wiaas_cm_extras_' . $this->package_id,
			$this->cl_extras);

		$cl_extras  = Wiaas_Package_CL_Pricing::get_extras($this->shop_owner_id, $this->package_id);

		$this->_validate_default_cl_extras($cl_extras);
	}

	/**
	 * @covers Wiaas_Package_CL_Pricing::get_extras_for_customer()
	 */
	function test_default_extras_retrieved_for_customer() {
		update_term_meta(
			$this->shop_owner_id,
			'_wiaas_cm_extras_' . $this->package_id,
			$this->cl_extras);

		$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());

		foreach ($pay_types as $pay_type) {
			$customer_cl_extra = Wiaas_Package_CL_Pricing::get_extras_for_customer(
				$this->shop_owner_id,
				$pay_type,
				$this->package_id,
				$this->customer_id);

			$this->assertNotNull($customer_cl_extra);

			$this->assertEquals(10, $customer_cl_extra['fixed']);
			$this->assertEquals(10, $customer_cl_extra['recurrent']);
			$this->assertEquals(10, $customer_cl_extra['services']);
		}
	}

	/**
	 * @covers Wiaas_Package_CL_Pricing::get_extras_for_customer()
	 */
	function test_customer_specific_extras_retrieved_for_customer() {
		// append customer specific extras for purchase payment type
		$this->cl_extras['purchase_customer_' . $this->customer_id] = array(
			'fixed' => 100,
			'recurrent' => 100,
			'services' => 100,
			'visible' => true
		);

		// set prices extras
		update_term_meta(
			$this->shop_owner_id,
			'_wiaas_cm_extras_' . $this->package_id,
			$this->cl_extras);

		// test that only extras are retrieved for purchase payment type
		// and the rest are not retrieved
		$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
		foreach ($pay_types as $pay_type) {
			$customer_cl_extra = Wiaas_Package_CL_Pricing::get_extras_for_customer(
				$this->shop_owner_id,
				$pay_type,
				$this->package_id,
				$this->customer_id);

			if ($pay_type === 'purchase') {

				$this->assertNotNull($customer_cl_extra);

				$this->assertEquals(100, $customer_cl_extra['fixed']);
				$this->assertEquals(100, $customer_cl_extra['recurrent']);
				$this->assertEquals(100, $customer_cl_extra['services']);
			} else {

				$this->assertNull($customer_cl_extra);
			}
		}
	}


	// HELPERS

	private function _validate_default_cl_extras($cl_extras) {
		$this->assertNotEmpty($cl_extras);

		$this->assertArrayHasKey('purchase_default', $cl_extras);
		$this->assertArrayHasKey('purchase_24_default', $cl_extras);
		$this->assertArrayHasKey('managed_36_default', $cl_extras);

		foreach ($cl_extras as $cl_extra) {
			$this->assertEquals(10, $cl_extra['fixed']);
			$this->assertEquals(10, $cl_extra['recurrent']);
			$this->assertEquals(10, $cl_extra['services']);
			$this->assertTrue($cl_extra['visible']);
		}
	}
}