<?php
/**
 * Processing history list table.
 *
 * @package AIAltTextGenerator
 * @since 2.2.0
 */

namespace AIAltTextGenerator;

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

if ( ! class_exists( 'WP_List_Table' ) ) {
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

/**
 * @since 2.2.0
 */
class HistoryListTable extends \WP_List_Table {

	public function __construct() {
		parent::__construct(
			[
				'singular' => 'history_item',
				'plural'   => 'history_items',
				'ajax'     => false,
			]
		);
	}

	/** @return array<string, string> */
	public function get_columns(): array {
		return [
			'cb'        => '<input type="checkbox" />',
			'date'      => __( 'Date', 'wp-powersuite' ),
			'image'     => __( 'Image', 'wp-powersuite' ),
			'status'    => __( 'Status', 'wp-powersuite' ),
			'old_alt'   => __( 'Previous alt', 'wp-powersuite' ),
			'new_alt'   => __( 'Generated alt', 'wp-powersuite' ),
		];
	}

	/** @return array<string, string> */
	protected function get_bulk_actions(): array {
		$actions = [];
		if ( Capabilities::canGenerate() ) {
			$actions['regenerate'] = __( 'Regenerate alt text', 'wp-powersuite' );
		}
		if ( Capabilities::canManage() ) {
			$actions['delete'] = __( 'Delete log entries', 'wp-powersuite' );
		}
		return $actions;
	}

	/** @param array<string, mixed> $item */
	protected function column_cb( $item ): string {
		$id = (int) ( $item['id'] ?? 0 );
		return sprintf( '<input type="checkbox" name="item[]" value="%d" />', $id );
	}

	/** @param array<string, mixed> $item */
	protected function column_date( $item ): string {
		return esc_html( (string) ( $item['created_at'] ?? '' ) );
	}

	/** @param array<string, mixed> $item */
	protected function column_image( $item ): string {
		$attachment_id = (int) ( $item['attachment_id'] ?? 0 );
		$filename      = (string) ( $item['filename'] ?? '' );
		$edit_url      = $attachment_id > 0 ? ( get_edit_post_link( $attachment_id, 'raw' ) ?: '' ) : '';
		$actions       = [];

		if ( $edit_url !== '' ) {
			$actions['edit'] = sprintf( '<a href="%s">%s</a>', esc_url( $edit_url ), esc_html__( 'Edit attachment', 'wp-powersuite' ) );
		}
		if ( Capabilities::canGenerate() && $attachment_id > 0 ) {
			$actions['regenerate'] = sprintf(
				'<a href="#" class="wpps-ai-alt-history-regenerate" data-attachment-id="%d">%s</a>',
				$attachment_id,
				esc_html__( 'Regenerate', 'wp-powersuite' )
			);
		}

		$label = '#' . $attachment_id;
		if ( $filename !== '' ) {
			$label .= '<br><span class="description">' . esc_html( $filename ) . '</span>';
		}

		$output = $edit_url !== '' ? '<a href="' . esc_url( $edit_url ) . '">' . $label . '</a>' : $label;
		if ( $actions !== [] ) {
			$output .= $this->row_actions( $actions );
		}

		return $output;
	}

	/** @param array<string, mixed> $item */
	protected function column_status( $item ): string {
		return esc_html( (string) ( $item['status'] ?? '' ) );
	}

	/** @param array<string, mixed> $item */
	protected function column_old_alt( $item ): string {
		$value = (string) ( $item['old_alt'] ?? '' );
		return $value !== '' ? esc_html( $value ) : '<span class="description">—</span>';
	}

	/** @param array<string, mixed> $item */
	protected function column_new_alt( $item ): string {
		$value = (string) ( $item['new_alt'] ?? '' );
		return $value !== '' ? esc_html( $value ) : '<span class="description">—</span>';
	}

	/** @param array<string, mixed> $item */
	public function column_default( $item, $column_name ) {
		return '';
	}

	public function prepare_items(): void {
		$per_page = 20;
		$page     = max( 1, $this->get_pagenum() );
		$search   = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['s'] ) ) : '';
		$total    = Logger::countEntries( $search );
		$rows     = Logger::getPage( $per_page, ( $page - 1 ) * $per_page, $search );

		$this->items = $rows;
		$this->set_pagination_args(
			[
				'total_items' => $total,
				'per_page'    => $per_page,
				'total_pages' => max( 1, (int) ceil( $total / $per_page ) ),
			]
		);
		$this->_column_headers = [ $this->get_columns(), [], $this->get_sortable_columns() ];
	}

	/** @return array<string, array<int, bool>> */
	public function get_sortable_columns(): array {
		return [];
	}
}
