<?php

defined( 'ABSPATH' ) || exit;

/**
 * Migration class template used by the wp cli to create migration classes.
 */

use Masteriyo\Database\Migration;

class Mailchimp extends Migration {
	/**
	 * Run the migration.
	 */
	public function up() {
		$old_settings = get_option( 'masteriyo_mailchimp_integration_setting', array() );

			$new_settings = array(
				'forced_email_subscription'  => isset( $old_settings['forced_email_subscription'] ) ? ( $old_settings['forced_email_subscription'] ? true : false ) : false,
				'is_connected'               => ! empty( $old_settings['api_key'] ) ? true : false,
				'api_key'                    => isset( $old_settings['api_key'] ) ? $old_settings['api_key'] : '',
				'list'                       => array(
					0       => '',
					'value' => '',
					'label' => '',
				),
				'group'                      => array(
					0       => '',
					'value' => '',
					'label' => '',
				),
				'interest'                   => array(),
				'subscriber_consent_message' => isset( $old_settings['subscriber_consent_message'] ) ? $old_settings['subscriber_consent_message'] : '',
			);

			// Set list data.
			if ( isset( $old_settings['default_list'] ) ) {
				$new_settings['list']['value'] = $old_settings['default_list']['value'];
				$new_settings['list']['label'] = $old_settings['default_list']['label'];
			}

			// Set group data.
			if ( isset( $old_settings['default_group'] ) ) {
				$new_settings['group']['value'] = $old_settings['default_group']['value'];
				$new_settings['group']['label'] = $old_settings['default_group']['label'];
			}

			// Set interests.
			if ( ! empty( $old_settings['default_group']['interests'] ) && is_array( $old_settings['default_group']['interests'] ) ) {
				foreach ( $old_settings['default_group']['interests'] as $interest ) {
					$new_interest               = array(
						0       => '',
						'value' => isset( $interest['value'] ) ? $interest['value'] : '',
						'label' => isset( $interest['label'] ) ? $interest['label'] : '',
					);
					$new_settings['interest'][] = $new_interest;
				}
			}

			 update_option( 'masteriyo_mailchimp_integration_setting', $new_settings );
	}

	/**
	 * Reverse the migrations.
	 */
	public function down() {
		$new_settings = get_option( 'masteriyo_mailchimp_integration_setting', array() );

		$old_settings = array(
			'forced_email_subscription'  => isset( $new_settings['forced_email_subscription'] ) && $new_settings['forced_email_subscription'] === true ? true : false,
			'api_key'                    => isset( $new_settings['api_key'] ) ? $new_settings['api_key'] : '',
			'subscriber_consent_message' => isset( $new_settings['subscriber_consent_message'] ) ? $new_settings['subscriber_consent_message'] : '',
			'default_group'              => array(),
			'default_list'               => array(
				'value'  => isset( $new_settings['list']['value'] ) ? $new_settings['list']['value'] : '',
				'label'  => isset( $new_settings['list']['label'] ) ? $new_settings['list']['label'] : '',
				'groups' => array(),
			),
		);

		// Set default group data
		if ( ! empty( $new_settings['group']['value'] ) ) {
			$old_settings['default_group'] = array(
				'value'     => $new_settings['group']['value'],
				'label'     => $new_settings['group']['label'],
				'interests' => array(),
			);
		}

		// Set interests
		if ( ! empty( $new_settings['interest'] ) && is_array( $new_settings['interest'] ) ) {
			foreach ( $new_settings['interest'] as $interest ) {
				if ( ! empty( $interest['value'] ) ) {
					$old_settings['default_group']['interests'][] = array(
						'value' => $interest['value'],
						'label' => $interest['label'],
					);
				}
			}
		}

		// Add group to default_list groups
		if ( ! empty( $old_settings['default_group'] ) ) {
			$old_settings['default_list']['groups'][] = $old_settings['default_group'];
		}

		update_option( 'masteriyo_mailchimp_integration_setting', $old_settings );
	}
}
