<?php
/**
 * Hook wiring for the admin post list translations UI.
 *
 * @package WPR_Translate
 */

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

/**
 * Enqueue admin styles for post list screens so the language columns inherit core sizing.
 *
 * @param string $hook Current admin hook.
 */
function wpr_translation_enqueue_post_list_assets( $hook ) {
    if ( 'edit.php' !== $hook ) {
        // Only load assets on the post list screen.
        return;
    }

    $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

    if ( ! $screen instanceof WP_Screen ) {
        // Invalid screen instance.
        return;
    }

    $post_type = $screen->post_type;

    if ( empty( $post_type ) || 'attachment' === $post_type ) {
        // Skip attachments and invalid post types.
        return;
    }

    $style_path    = WPR_TRANSLATE_PLUGIN_ROOT . 'assets/css/admin.css';
    $style_version = file_exists( $style_path ) ? filemtime( $style_path ) : WPR_TRANSLATE_VERSION;

    if ( ! wp_style_is( 'wpr-translate-admin', 'enqueued' ) ) {
        // Load the admin stylesheet so the language column matches core list table sizing.
        wp_enqueue_style(
            'wpr-translate-admin',
            WPR_TRANSLATE_PLUGIN_URL . 'assets/css/admin.css',
            array(),
            $style_version
        );
    }
}
add_action( 'admin_enqueue_scripts', 'wpr_translation_enqueue_post_list_assets' );

/**
 * Register language column hooks for the current screen.
 *
 * @param WP_Screen $screen Current admin screen instance.
 */
function wpr_translation_register_post_list_hooks( $screen ) {
    if ( ! $screen instanceof WP_Screen ) {
        // Bail out when an invalid screen is supplied.
        $context = is_object( $screen ) ? get_class( $screen ) : gettype( $screen );
        return;
    }

    $post_type = $screen->post_type;

    if ( empty( $post_type ) || 'attachment' === $post_type ) {
        // Skip attachments and invalid post types.
        return;
    }

    // Register column and view hooks for the current post type.
    add_filter( "manage_edit-{$post_type}_columns", 'wpr_translation_register_language_column', 0 );
    add_filter( "manage_{$post_type}_posts_columns", 'wpr_translation_register_language_column', 0 );
    add_filter( "manage_edit-{$post_type}_sortable_columns", 'wpr_translation_register_sortable_language_column', 10 );
    if ( 'post' === $post_type ) {
        // Hook into core post column rendering.
        add_action( 'manage_posts_custom_column', 'wpr_translation_render_language_column', 0, 2 );
    } elseif ( 'page' === $post_type ) {
        // Hook into core page column rendering.
        add_action( 'manage_pages_custom_column', 'wpr_translation_render_language_column', 0, 2 );
    } else {
        // Hook into custom post type column rendering.
        add_action( "manage_{$post_type}_posts_custom_column", 'wpr_translation_render_language_column', 0, 2 );
    }
    add_filter( "views_edit-{$post_type}", 'wpr_translation_register_language_views', 20 );

    // Core post types use generic hooks instead of the custom post type hook.
}
add_action( 'current_screen', 'wpr_translation_register_post_list_hooks' );
add_action( 'restrict_manage_posts', 'wpr_translation_render_language_filter_control' );
add_action( 'pre_get_posts', 'wpr_translation_filter_admin_query_by_language' );

/**
 * Adds legacy-like column class aliases on post list tables.
 *
 * Keeps plugin language column key stable while exposing the common
 * `column-language` class alias used on property list screens.
 */
function wpr_translation_add_post_list_column_class_aliases() {
    $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

    if ( ! $screen instanceof WP_Screen || 'edit' !== $screen->base ) {
        // Only handle post list tables.
        return;
    }

    if ( empty( $screen->post_type ) || 'attachment' === $screen->post_type ) {
        // Skip invalid post types and attachment list tables.
        return;
    }

    echo '<script>';
    echo 'document.addEventListener("DOMContentLoaded",function(){';
    echo 'var header=document.getElementById("wpr_language");';
    echo 'if(header){header.classList.add("column-language");}';
    echo 'document.querySelectorAll("th.column-wpr_language,td.column-wpr_language").forEach(function(cell){';
    echo 'cell.classList.add("column-language");';
    echo '});';
    echo '});';
    echo '</script>';
}
add_action( 'admin_head-edit.php', 'wpr_translation_add_post_list_column_class_aliases' );

add_action( 'admin_head-post.php', 'wpr_translation_render_edit_screen_language_badge' );
add_action( 'admin_head-post-new.php', 'wpr_translation_render_edit_screen_language_badge' );
