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

/**
 * Register the custom column for allowed post types
 */
function cafi_add_custom_column( $columns ) {
	$columns['cafi_status'] = __( 'Auto Featured Image', 'custom-auto-featured-image' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
	return $columns;
}

/**
 * Render the content of the custom column
 */
function cafi_render_custom_column( $column, $post_id ) {
	if ( 'cafi_status' === $column ) {
		$thumbnail_id = get_post_thumbnail_id( $post_id );
		$is_generated = $thumbnail_id ? get_post_meta( $thumbnail_id, '_cafi_generated', true ) : false;
        
        $status_text = $is_generated ? __( 'Generated', 'custom-auto-featured-image' ) : __( 'Not Generated', 'custom-auto-featured-image' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
        $status_color = $is_generated ? '#46b450' : '#dc3232';
        $status_icon = $is_generated ? 'dashicons-yes' : 'dashicons-no-alt';
        $btn_text = $is_generated ? __( 'Update', 'custom-auto-featured-image' ) : __( 'Generate', 'custom-auto-featured-image' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
        $btn_class = $is_generated ? '' : 'button-primary';

        echo '<div id="afi-col-' . esc_attr( $post_id ) . '">';
            echo '<span class="dashicons ' . esc_attr( $status_icon ) . '" style="color: ' . esc_attr( $status_color ) . ';"></span> <span class="afi-status-label">' . esc_html( $status_text ) . '</span>';
            echo '<div style="margin-top: 5px; display: flex; align-items: center; gap: 5px;">';
                echo '<button class="button button-small afi-gen-btn ' . esc_attr( $btn_class ) . '" data-post-id="' . esc_attr( $post_id ) . '" data-nonce="' . esc_attr( wp_create_nonce( 'cafi_generate_' . $post_id ) ) . '" style="font-size: 11px; height: 24px; line-height: 22px; padding: 0 8px;">' . esc_html( $btn_text ) . '</button>';
                echo '<span class="spinner" style="float: none; margin: 0; visibility: hidden;"></span>';
            echo '</div>';
        echo '</div>';
	}
}

/**
 * Enqueue scripts for the post list
 */
function cafi_enqueue_post_list_scripts( $hook ) {
    $screen = get_current_screen();
    // Check if we are on the edit screen for allowed post types or just 'edit.php' abstractly
    if ( 'edit.php' !== $hook ) {
        return;
    }
    
    // We could further check the post type if strictness is needed, but hook check covers the page load.
    
    wp_enqueue_script( 
        'cafi-post-list-js', 
        plugin_dir_url( __FILE__ ) . 'assets/post-list.js', 
        array( 'jquery' ), 
        '1.0.0', 
        true 
    );
}
add_action( 'admin_enqueue_scripts', 'cafi_enqueue_post_list_scripts' );

/**
 * Initialize the hooks
 */
function cafi_init_post_list_hooks() {
	$options = get_option( 'cafi_settings' );
	$allowed_types = isset( $options['post_types'] ) && is_array( $options['post_types'] ) && !empty( $options['post_types'] ) ? $options['post_types'] : ['post'];

	foreach ( $allowed_types as $post_type ) {
		add_filter( "manage_{$post_type}_posts_columns", 'cafi_add_custom_column' );
		add_action( "manage_{$post_type}_posts_custom_column", 'cafi_render_custom_column', 10, 2 );
	}
}
add_action( 'admin_init', 'cafi_init_post_list_hooks' );
