<?php

class Wiaas_Unit_Test_Case extends WP_UnitTestCase {

	/**
	 * Executes any db migrations that are done to
	 * `wp_options` table since it is deleted on every execution
	 */
    function setUp() {
        parent::setUp();
        wp_set_current_user(1);

        # Setup Gravity info since options table is deleted after each test
	    gf_upgrade()->install();

	    WC_PB_Install::install();

	    wiaas_db_update_setup_gravity();

		wiaas_db_update_enable_orders_access_management();

		Wiaas_Countries::register_product_countries_taxonomy();

		Wiaas_Product_Category::register_product_categories();

		Wiaas_Package_Type::register_package_type_taxonomy();

		Wiaas_Order_Project::register_order_project_taxonomy();

		Wiaas_Product_Supplier::register_supplier_taxonomy();
		
		define('WP_TEST_IN_PROGRESS',true);
    }

	function tearDown() {
		parent::tearDown();
	}

	function create_new_product($price = 10, $is_recurring = false, $pay_period = 0) {
    	$post_id = wp_insert_post(array(
		    'post_type' => 'product',
		    'post_status' => 'publish',
		    'post_name' => 'product',
		    'post_title' => 'Product',
		    'post_content' => 'Product',
		    'post_excerpt' => 'Product'
	    ), true);

    	$product =  new WC_Product_Simple($post_id);

		Wiaas_Product_Pricing::set_product_price($product, $price, $is_recurring,  $pay_period);

		$product->save();

    	return $product;
	}

	function create_new_package() {
		$post_id = wp_insert_post(array(
			'post_type' => 'product',
			'post_status' => 'publish',
			'post_name' => 'product',
			'post_title' => 'Package',
			'post_content' => 'Package',
			'post_excerpt' => 'Package'
		), true);

		return new WC_Product_Bundle($post_id);
	}

	function add_product_category($product, $name) {
		wp_set_object_terms($product->get_id(), $name, 'product_cat', false);
		clean_object_term_cache( $product->get_id(), 'product_cat' );
	}

	function add_products_to_package($package, $products) {
    	$bundled_data = array();

    	foreach ($products as $product) {
    		$bundled_data[] = array(
			    'product_id' => $product->get_id(),
			    'quantity_min' => 1,
			    'quantity_max' => 1,
			    'priced_individually' => true
		    );
	    }

		$package->set_bundled_data_items($bundled_data);

		$package->sync(true);
	}


    function create_new_wiaas_template() {
        $post_id = wp_insert_post(array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'post_name' => 'product',
            'post_title' => 'Package',
            'post_content' => 'Package',
            'post_excerpt' => 'Package'
        ), true);

        return new WC_Product_Template($post_id);

    }

	function create_new_customer($login = 'customer_test', $organization_name = 'test-customer-organization') {
		$customer_id = wp_insert_user(array(
			'user_login' => $login,
			'user_pass'  => 'test',
			'user_email' => $login . '@mail.com',
			'role'       => 'customer',
		));

		$organization = get_term_by('name', $organization_name, Wiaas_User_Organization::TAXONOMY_NAME);
		if (is_wp_error($organization)) {
			$organization = wp_insert_term(
				$organization_name,
				Wiaas_User_Organization::TAXONOMY_NAME
			);
		}

		wp_set_terms_for_user(
			$customer_id,
			Wiaas_User_Organization::TAXONOMY_NAME,
			[$organization_name]);

		return $customer_id;
	}
}