<?php
namespace KBT_SEO;

if ( ! defined( 'ABSPATH' ) ) exit;

class Meta_Box {

	public static function init() {
		add_action( 'add_meta_boxes', array( __CLASS__, 'register' ) );
		add_action( 'save_post', array( __CLASS__, 'save' ), 10, 2 );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) );
		add_action( 'wp_ajax_kbt_seo_analyze', array( __CLASS__, 'ajax_analyze' ) );
	}

	public static function register() {
		add_meta_box(
			'kbt-seo-assistant',
			'KBT SEO Assistant',
			array( __CLASS__, 'render' ),
			'post',
			'side',
			'high'
		);
	}

	public static function assets( $hook ) {
		if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) return;
		$screen = get_current_screen();
		if ( ! $screen || 'post' !== $screen->post_type ) return;

		wp_enqueue_style(
			'kbt-seo-metabox',
			KBT_SEO_URL . 'assets/metabox.css',
			array(),
			KBT_SEO_VERSION
		);
		wp_enqueue_script(
			'kbt-seo-metabox',
			KBT_SEO_URL . 'assets/metabox.js',
			array( 'jquery' ),
			KBT_SEO_VERSION,
			true
		);
		wp_localize_script( 'kbt-seo-metabox', 'kbtSeo', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'kbt_seo_analyze' ),
			'post_id'  => get_the_ID(),
		) );
	}

	public static function render( $post ) {
		$keyword = get_post_meta( $post->ID, '_kbt_focus_keyword', true );
		wp_nonce_field( 'kbt_seo_save', 'kbt_seo_nonce' );
		?>
		<div id="kbt-seo-panel">
			<div class="kbt-seo-field">
				<label for="kbt-focus-keyword"><strong>Focus Keyword</strong></label>
				<input type="text" id="kbt-focus-keyword" name="kbt_focus_keyword"
					   value="<?php echo esc_attr( $keyword ); ?>"
					   placeholder="Masukkan kata kunci utama..."
					   autocomplete="off" />
			</div>

			<div class="kbt-seo-score-wrap">
				<div class="kbt-seo-score-circle" id="kbt-score-circle">
					<span id="kbt-score-number">--</span>
				</div>
				<div class="kbt-seo-score-label">Skor SEO</div>
			</div>

			<button type="button" class="button button-primary" id="kbt-analyze-btn" style="width:100%;margin-bottom:12px;">
				Analisis SEO
			</button>

			<div id="kbt-seo-results"></div>

			<div class="kbt-seo-checklist" id="kbt-seo-checklist" style="display:none;">
				<h4>Checklist Sebelum Publish</h4>
				<label><input type="checkbox" /> Focus keyword sudah diisi</label>
				<label><input type="checkbox" /> Featured image sudah dipasang</label>
				<label><input type="checkbox" /> Meta description sudah ditulis</label>
				<label><input type="checkbox" /> Minimal 1 internal link</label>
				<label><input type="checkbox" /> Alt text di semua gambar</label>
				<label><input type="checkbox" /> Kategori sudah dipilih</label>
			</div>
		</div>
		<?php
	}

	public static function save( $post_id, $post ) {
		if ( ! isset( $_POST['kbt_seo_nonce'] ) || ! wp_verify_nonce( $_POST['kbt_seo_nonce'], 'kbt_seo_save' ) ) return;
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
		if ( ! current_user_can( 'edit_post', $post_id ) ) return;

		if ( isset( $_POST['kbt_focus_keyword'] ) ) {
			update_post_meta( $post_id, '_kbt_focus_keyword', sanitize_text_field( $_POST['kbt_focus_keyword'] ) );
		}
	}

	public static function ajax_analyze() {
		check_ajax_referer( 'kbt_seo_analyze', 'nonce' );

		$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
		$keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( $_POST['keyword'] ) : '';

		if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) {
			wp_send_json_error( 'Invalid post.' );
		}

		// Save keyword
		update_post_meta( $post_id, '_kbt_focus_keyword', $keyword );

		$result = SEO_Analyzer::analyze( $post_id, $keyword );
		wp_send_json_success( $result );
	}
}
