<?php

namespace Waypoint\Agent\Setup;

use Waypoint\Agent\Base\Thing;

class Default_Content extends Thing {

	public function attach_hooks(): void {
		add_action( 'init', [ $this, 'maybe_delete_hello_world' ], 100 );
	}

	public function maybe_delete_hello_world(): void {
		if ( get_option( 'waypoint_default_content_cleanup_ran' ) ) {
			return;
		}

		$post = get_post( 1 );

		if ( $post instanceof \WP_Post ) {
			$content = wp_strip_all_tags( $post->post_content );
			$content = preg_replace( '/\s+/', ' ', trim( $content ) );

			$is_default = $post->ID === 1
				&& $post->post_type === 'post'
				&& $post->post_name === 'hello-world'
				&& $post->post_title === 'Hello world!'
				&& str_contains(
					$content,
					'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!'
				);

			if ( $is_default ) {
				wp_delete_post( $post->ID, true );
			}
		}

		update_option( 'waypoint_default_content_cleanup_ran', time(), false );
	}
}
