<?php
/**
 * Swish Plugin Update class.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout\Gateways\Swish;

use Flo_Checkout\Shared;

defined( 'ABSPATH' ) || exit;

/**
 * Swish Plugin Update class.
 */
class Swish_Plugin_Update {


	/**
	 * Handles update for Swish gateway settings.
	 *
	 * TODO: Replace with Shared updater.
	 *
	 * @return void
	 */
	public static function handle_gateway_update(): void {

		$stored_version = get_option( 'mf_checkout_version' );

		if ( Shared::instance()->info->package_data->get_version() === $stored_version ) {
			return; // Already updated.
		}

		/**
		 * Backwards compatibility for plugin pre v1.7.0.
		 */
		if ( version_compare( $stored_version, '1.7.0', '<' ) ) {
			/**
			 * Check Swish Environment.
			 */
			$old_sandbox_val = Swish_Settings::get( 'sandbox' );
			$old_mss_val     = Swish_Settings::get( 'sandbox_mss' );

			if ( 'no' !== $old_mss_val ) {
				$old_env = 'mss';
			} elseif ( 'no' !== $old_sandbox_val ) {
				$old_env = 'sandbox';
			} else {
				$old_env = 'prod';
			}

			Swish_Settings::update( 'swish_environment', $old_env );

			/**
			 * Check Swish payee number.
			 */
			if ( empty( Swish_Settings::get( 'sandbox_payee_number' ) ) ) {
				Swish_Settings::update( 'sandbox_payee_number', Swish_Settings::get( 'payee_number' ) );
			}

			/**
			 * Check certificates.
			 */
			// Check if there is a .key file with the same name as the public certificate file for production settings.
			$prod_public_cert = Swish_Settings::get( 'live_certificate' );
			if ( $prod_public_cert ) {
				$prod_public_cert_info  = pathinfo( $prod_public_cert );
				$prod_private_cert_path = $prod_public_cert_info['dirname'] . '/' . $prod_public_cert_info['filename'] . '.key';

				if ( file_exists( $prod_private_cert_path ) ) {
					Swish_Settings::update( 'live_private_certificate', $prod_private_cert_path );
				}
			}

			// Check if there is a .key file with the same name as the public certificate file for sandbox settings.
			$sandbox_public_cert = Swish_Settings::get( 'sandbox_certificate' );
			if ( $sandbox_public_cert ) {
				$sandbox_public_cert_info  = pathinfo( $sandbox_public_cert );
				$sandbox_private_cert_path = $sandbox_public_cert_info['dirname'] . '/' . $sandbox_public_cert_info['filename'] . '.key';

				if ( file_exists( $sandbox_private_cert_path ) ) {
					Swish_Settings::update( 'sandbox_private_certificate', $sandbox_private_cert_path );
				}
			}
		}

		/**
		 * Backwards compatibility for plugin pre v1.7.2.
		 */
		if ( version_compare( $stored_version, '1.7.2', '<' ) ) {
			// Copy the old certificates to the new location.
			self::copy_cert_file( 'public' );
			self::copy_cert_file( 'private' );
			self::copy_cert_file( 'sandbox_public' );
			self::copy_cert_file( 'sandbox_private' );
		}

		/**
		 * Backwards compatibility for plugin pre v2.0.0.
		 */
		if ( version_compare( $stored_version, '2.0.0', '<' ) ) {
			// Rename wp-uploads subfolder from moreflo-checkout to northmill-flo-checkout.
			$old_certs_path = wp_upload_dir( null, false )['basedir'] . '/moreflo-checkout';
			$new_certs_path = wp_upload_dir( null, false )['basedir'] . '/northmill-flo-checkout';

			if ( is_dir( $old_certs_path ) && ! is_dir( $new_certs_path ) ) {
				rename( $old_certs_path, $new_certs_path );
			}
		}

		/**
		 * Update finished.
		 */
		update_option( 'mf_checkout_version', Shared::instance()->info->package_data->get_version() );
	}

	/**
	 * Copy certificate file to new location.
	 *
	 * @param string $cert_type Type of certificate file to copy.
	 */
	private static function copy_cert_file( string $cert_type ): void {
		$cert_info = Swish_Certificates::get_cert_info_by_type( $cert_type );

		if ( empty( $cert_info['option_name'] ) ) {
			return;
		}
		$current_path = Swish_Settings::get( $cert_info['option_name'] );

		if ( empty( $current_path ) || ! is_file( $current_path ) ) {
			return;
		}

		if ( empty( $cert_info['filepath'] ) ) {
			return;
		}
		$path_parts = pathinfo( $current_path );
		$new_path   = $cert_info['filepath'] . '.' . $path_parts['extension'];

		if ( $new_path !== $current_path ) {
			if ( ! copy( $current_path, $new_path ) ) {
				return;
			}

			! empty( $cert_info['option_name'] ) && Swish_Settings::update( $cert_info['option_name'], $new_path );
		}
	}
}
