<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! function_exists( 'drop_print_enroll_shipping_zones' ) ) {
	function drop_print_enroll_shipping_zones( int $client_uid = 0 ): int {
		if ( ! class_exists( 'WC_Shipping_Zones' ) || ! class_exists( 'WC_Shipping_Zone' ) ) {
			return 0;
		}
		$zone_ids   = wp_list_pluck( WC_Shipping_Zones::get_zones(), 'id' );
		$zone_ids[] = 0;
		$present = 0;
		foreach ( $zone_ids as $zone_id ) {
			$zone = new WC_Shipping_Zone( (int) $zone_id );
			$has = false;
			foreach ( $zone->get_shipping_methods() as $method ) {
				if ( 'drop_print_live_postage' === $method->id ) {
					$has = true;
					break;
				}
			}
			if ( ! $has ) {
				$has = (bool) $zone->add_shipping_method( 'drop_print_live_postage' );
			}
			if ( $has ) {
				$present++;
			}
		}
		if ( $present > 0 ) {
			update_option( 'drop_print_shipping_zones_enrolled', 1 );
		} else {
			delete_option( 'drop_print_shipping_zones_enrolled' );
		}
		return $present;
	}
}
if ( ! function_exists( 'drop_print_enroll_shipping_zones_on_connect' ) ) {
	function drop_print_enroll_shipping_zones_on_connect( $client_uid = 0 ) {
		if ( drop_print_shipping_reminder_is_snoozed() ) {
			return;
		}
		drop_print_enroll_shipping_zones( (int) $client_uid );
	}
}
add_action( 'drop_print_connected', 'drop_print_enroll_shipping_zones_on_connect' );
if ( ! function_exists( 'drop_print_shipping_zone_report' ) ) {
	function drop_print_shipping_zone_report(): array {
		$report = array( 'wc_active' => false, 'zone_count' => 0, 'enrolled' => false, 'has_any' => false );
		if ( ! class_exists( 'WC_Shipping_Zones' ) || ! class_exists( 'WC_Shipping_Zone' ) ) {
			return $report;
		}
		$report['wc_active'] = true;
		$zones                = WC_Shipping_Zones::get_zones();
		$report['zone_count'] = count( $zones );
		$zone_ids   = wp_list_pluck( $zones, 'id' );
		$zone_ids[] = 0;
		foreach ( $zone_ids as $zone_id ) {
			$zone = new WC_Shipping_Zone( (int) $zone_id );
			foreach ( $zone->get_shipping_methods() as $method ) {
				$report['has_any'] = true;
				if ( 'drop_print_live_postage' === $method->id ) {
					$report['enrolled'] = true;
				}
			}
		}
		return $report;
	}
}
if ( ! function_exists( 'drop_print_shipping_reminder_is_snoozed' ) ) {
	function drop_print_shipping_reminder_is_snoozed(): bool {
		return (int) get_option( 'drop_print_shipping_reminder_snooze_until', 0 ) > time();
	}
}
if ( ! function_exists( 'drop_print_shipping_setup_notice' ) ) {
	function drop_print_shipping_setup_notice() {
		if ( ! current_user_can( 'manage_options' ) || ! function_exists( 'drop_print_onboarding_page_url' ) ) {
			return;
		}
		if ( drop_print_shipping_reminder_is_snoozed() ) {
			return;
		}
		if ( ! function_exists( 'drop_print_onboarding_user_is_connected' ) || ! drop_print_onboarding_user_is_connected( get_current_user_id() ) ) {
			return;
		}
		$report = drop_print_shipping_zone_report();
		if ( ! $report['wc_active'] || $report['enrolled'] ) {
			return;
		}
		$snooze_url = static function ( $days ) {
			return wp_nonce_url(
				add_query_arg( 'drop_print_shipping_snooze', (int) $days ),
				'drop_print_shipping_snooze'
			);
		};
		?>
		<div class="notice notice-warning">
			<p>
				<strong><?php esc_html_e( 'Drop Print shipping isn’t set up yet.', 'drop-print' ); ?></strong>
				<?php
				echo ' ';
				if ( ! $report['has_any'] ) {
					esc_html_e( 'Your shop has no shipping methods at all, so a customer can’t complete an order for anything physical.', 'drop-print' );
				} else {
					esc_html_e( 'Prints won’t be quoted at their real delivery cost until Drop Print’s live rate is added to your shipping zones.', 'drop-print' );
				}
				?>
			</p>
			<p>
				<a href="<?php echo esc_url( drop_print_onboarding_page_url( 'shipping' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Enable shipping for Drop Print', 'drop-print' ); ?></a>
				<a href="<?php echo esc_url( $snooze_url( 1 ) ); ?>" style="margin-left:12px;"><?php esc_html_e( 'Remind me tomorrow', 'drop-print' ); ?></a>
				<a href="<?php echo esc_url( $snooze_url( 7 ) ); ?>" style="margin-left:12px;"><?php esc_html_e( 'Remind me in a week', 'drop-print' ); ?></a>
			</p>
			<p class="description">
				<?php
				printf(
					esc_html__( 'Prefer to do it yourself? Add “Drop Print Live Postage” to a zone under %s.', 'drop-print' ),
					'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=shipping' ) ) . '">' . esc_html__( 'WooCommerce → Settings → Shipping', 'drop-print' ) . '</a>'
				);
				?>
			</p>
		</div>
		<?php
	}
}
add_action( 'admin_notices', 'drop_print_shipping_setup_notice' );
if ( ! function_exists( 'drop_print_shipping_notice_snooze' ) ) {
	function drop_print_shipping_notice_snooze() {
		if ( empty( $_GET['drop_print_shipping_snooze'] ) || ! current_user_can( 'manage_options' ) ) {
			return;
		}
		check_admin_referer( 'drop_print_shipping_snooze' );
		$days = absint( wp_unslash( $_GET['drop_print_shipping_snooze'] ) );
		$days = max( 1, min( 7, $days ) );
		update_option( 'drop_print_shipping_reminder_snooze_until', time() + ( $days * DAY_IN_SECONDS ) );
		wp_safe_redirect( remove_query_arg( array( 'drop_print_shipping_snooze', '_wpnonce' ) ) );
		exit;
	}
}
add_action( 'admin_init', 'drop_print_shipping_notice_snooze' );
