<?php

defined('ABSPATH') || exit;

/**
 * Fired during plugin activation
 *
 * @link       test.example.com
 * @since      1.0.0
 *
 * @package    Primer
 * @subpackage Primer/includes
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Primer
 * @subpackage Primer/includes
 * @author     test_user <testwe@gmail.com>
 */
class Primer_Activator {

    /**
     * Short Description. (use period)
     *
     * Long Description.
     *
     * @since    1.0.0
     */
    public static function activate() {
        global $wpdb, $wp_version;

        $require = array(
            'wordpress' => '4.0',
            'php'       => '5.3',
            'curl'      => true,
        );

        $php_version = phpversion();
        $extensions  = get_loaded_extensions();
        $curl        = in_array('curl', $extensions);
        $error       = array();

        if ($wp_version < $require['wordpress']) {
            $error['wp_error'] = 'yes';
        }
        if ($php_version < $require['php']) {
            $error['php_error'] = 'yes';
        }
        if ($curl != true) {
            $error['curl_error'] = 'yes';
        }


        set_transient('primer_activation_warning', $error, 5);

        $orders = wc_get_orders(array(
            'date_created' => '>' . '01-01-2022',
            'limit'        => 1000
        ));

        foreach ($orders as $order) {
            if(!$order) {
                continue;
            }
            if (is_a($order, 'WC_Order_Refund')) {
                $order = wc_get_order($order->get_parent_id());
            }
            $receipt_order_status = $order->get_meta('receipt_status');
            if (empty($receipt_order_status)) {
                $order->update_meta_data('receipt_status', 'not_issued');
                $order->save();
            }
        }

        $mydata_options          = get_option('primer_mydata');
        $licenses                = get_option('primer_licenses');
        $category_default_option = (!isset($licenses['productKind']) || $licenses['productKind'] == 'goods') ? 'Goods' : 'Services';

        if (!isset($mydata_options['predefined_category'])) {
            $mydata_options['predefined_category'] = $category_default_option;
            update_option('primer_mydata', $mydata_options, true);
        }

        $reset_series  = array('EMPTY', 'A', 'B', 'C', 'D', 'E', 'Z', 'H', 'Q', 'I', 'K', 'L', 'M', 'N', 'J', 'O', 'P', 'R', 'S', 'T', 'Y', 'F', 'X', 'W', 'V');

        $invoices_keys = ['invoice_numbering_gr_series', 'invoice_numbering_gi_series', 'invoice_numbering_within_series', 'invoice_numbering_outside_series',
                          'invoice_numbering_gr_test_api_series', 'invoice_numbering_gi_test_api_series', 'invoice_numbering_within_test_api_series', 'invoice_numbering_outside_test_api_series',
                          'credit_receipt_series', 'credit_invoice_series', 'credit_receipt_test_api_series', 'credit_invoice_test_api_series'
        ];

        foreach ($invoices_keys as $invoice_key) {
            $mydata_options[$invoice_key] = $mydata_options[$invoice_key] ?? 'EMPTY';
        }

        $prefixes = ['invoice_numbering_gr_', 'invoice_numbering_gi_', 'invoice_numbering_gi_', 'invoice_numbering_within_', 'invoice_numbering_outside_', 'credit_receipt_', 'credit_invoice_'];
        $suffixes = ['', '_test_api'];

        foreach ($reset_series as $reset_serie) {
            foreach ($prefixes as $prefix) {
                foreach ($suffixes as $suffix) {
                    $key = $prefix . $reset_serie . $suffix;
                    if (!isset($mydata_options[$key])) {
                        $mydata_options[$key] = 1;
                    }
                }
            }
            update_option('primer_mydata', $mydata_options, true);
        }

	}

}
