<?php

class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case {

	/**
	 * @covers Wiaas_Package_Status::register_package_status_taxonomy()
	 * @covers Wiaas_Package_Status::get_available_package_statuses()
	 */
	function test_register_package_status_taxonomy() {
		$taxonomy = get_taxonomy('package_status');

		$this->assertInstanceOf(WP_Taxonomy::class, $taxonomy);

		$package_statuses = Wiaas_Package_Status::get_available_package_statuses();

		$this->assertNotEmpty($package_statuses);

		$this->assertContains(Wiaas_Package_Status::AVAILABLE, $package_statuses);
	}

	/**
	 * @covers Wiaas_Package_Type::set_package_status()
	 * @covers Wiaas_Package_Type::get_package_status()
	 */
	function test_adding_package_status() {
		$package = $this->create_new_package();

		Wiaas_Package_Status::set_package_status($package->get_id(), Wiaas_Package_Status::AVAILABLE);

		$package_status = Wiaas_Package_Status::get_package_status($package->get_id());

		$this->assertNotNull($package_status);
		$this->assertEquals($package_status, Wiaas_Package_Status::AVAILABLE);
    }

     /**
     * Test package status update on simple product price update
     */
    function test_package_status_update_on_simple_product_price_update() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(20);
		$this->add_product_category($product2, 'software');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2));

        $pricing_rules = array(
			'purchase' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 0,
				'minimal_services_price' => 0
			),
			'purchase_24' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 0,
				'minimal_services_price' => 100
			),
			'managed_36' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 100,
				'minimal_services_price' => 100
			)
		);
		$commision = 50;
		$cost_margin = 100;
        
        Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin);
		$this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::AVAILABLE);

        $product1->set_price(1000);
        $this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::MARGIN_EXCEEDED);

    }
    
    /**
     * Test package status update on cost margin update
     */
    function test_package_status_update_on_margin_cost_update() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(20);
		$this->add_product_category($product2, 'software');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2));

        $pricing_rules = array(
			'purchase' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 0,
				'minimal_services_price' => 0
			),
			'purchase_24' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 0,
				'minimal_services_price' => 100
			),
			'managed_36' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 100,
				'minimal_services_price' => 100
			)
		);
		$commision = 50;
		$cost_margin = 0;
        
        Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin);
		$this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::AVAILABLE);

        $cost_margin = 1;
        Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin);
		$this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::MARGIN_EXCEEDED);

    }
}