<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/* -------------------------------------------------------------------------
 * Options (single site)
 * ---------------------------------------------------------------------- */

function misticease_get_default_options(): array {
	return [
		'thumbnail_max_width' => 640,
		'popup_max_width'     => 854,
		'lazy_load'           => 'on',
		'cache_quota_mb'      => 0,
		'cache_version'       => 1,
		'used_cache_bytes'    => 0,
		'image_popup'         => 'on',
		'floating_ux'        => 'on',
		'cdn_hosts'           => [],
	];
}

function misticease_opts_get(): array {
	$defaults = misticease_get_default_options();
	$opt      = get_option( 'misticease_options', [] );
	return array_merge( $defaults, is_array( $opt ) ? $opt : [] );
}

function misticease_opts_update( array $patch ): array {
	$cur  = misticease_opts_get();
	$next = array_merge( $cur, $patch );
	update_option( 'misticease_options', $next, 'no' );
	return $next;
}

function misticease_cdn_hosts_effective(): array {
	if ( is_multisite() && (bool) get_site_option( 'misticease_network_enforce_cdn', false ) ) {
		$net = get_site_option( 'misticease_network_cdn_hosts', [] );
		return is_array( $net ) ? array_values( array_filter( array_map( 'strval', $net ) ) ) : [];
	}
	$o = misticease_opts_get();
	return ( isset( $o['cdn_hosts'] ) && is_array( $o['cdn_hosts'] ) ) ? $o['cdn_hosts'] : [];
}

function misticease_opts_inc_ver(): void {
	$o                   = misticease_opts_get();
	$current             = isset( $o['cache_version'] ) ? (int) $o['cache_version'] : 1;
	$o['cache_version']  = $current + 1;
	update_option( 'misticease_options', $o, 'no' );
}


add_filter(
	'pre_update_option_misticease_options',
	static function ( $new, $old ) {
		$new = is_array( $new ) ? $new : [];
		$old = is_array( $old ) ? $old : [];

		$keys = [ 'thumbnail_max_width', 'popup_max_width', 'lazy_load', 'cache_quota_mb', 'image_popup', 'floating_ux', 'cdn_hosts' ];
		$changed = false;

		foreach ( $keys as $k ) {
			$ov = $old[ $k ] ?? null;
			$nv = array_key_exists( $k, $new ) ? $new[ $k ] : $ov;
			if ( $ov !== $nv ) {
				$changed = true;
			}
			$new[ $k ] = $nv;
		}

		$old_ver       = (int) ( $old['cache_version'] ?? 1 );
		$requested_ver = array_key_exists( 'cache_version', $new ) ? (int) $new['cache_version'] : $old_ver;
		$new['cache_version'] = $changed ? $old_ver + 1 : max( $old_ver, $requested_ver );

		if ( ! isset( $new['used_cache_bytes'] ) && isset( $old['used_cache_bytes'] ) ) {
			$new['used_cache_bytes'] = (int) $old['used_cache_bytes'];
		}

		return $new;
	},
	10,
	2
);

function misticease_bump_cache_version_on_post_updated( int $post_id, WP_Post $post_after, WP_Post $post_before ): void {
	if ( $post_id <= 0 ) {
		return;
	}
	if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
		return;
	}
	if ( ! post_type_supports( $post_after->post_type, 'editor' ) ) {
		return;
	}
	if ( 'auto-draft' === $post_after->post_status || 'inherit' === $post_after->post_status ) {
		return;
	}

	$fields = [
		'post_title',
		'post_content',
		'post_excerpt',
		'post_status',
		'post_name',
		'post_password',
		'post_parent',
		'menu_order',
	];

	foreach ( $fields as $field ) {
		if ( (string) $post_after->{$field} !== (string) $post_before->{$field} ) {
			misticease_opts_inc_ver();
			return;
		}
	}
}
add_action( 'post_updated', 'misticease_bump_cache_version_on_post_updated', 10, 3 );

function misticease_bump_cache_version_on_post_delete( int $post_id ): void {
	if ( $post_id <= 0 ) {
		return;
	}
	if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
		return;
	}

	$post = get_post( $post_id );
	if ( ! $post instanceof WP_Post ) {
		return;
	}
	if ( 'auto-draft' === $post->post_status || 'inherit' === $post->post_status || ! post_type_supports( $post->post_type, 'editor' ) ) {
		return;
	}

	misticease_opts_inc_ver();
}
add_action( 'before_delete_post', 'misticease_bump_cache_version_on_post_delete', 10, 1 );