<?php
/**
 * Review queue 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 ReviewListTable extends \WP_List_Table {

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

	/** @return array<string, string> */
	public function get_columns(): array {
		return [
			'cb'         => '<input type="checkbox" />',
			'image'      => __( 'Image', 'wp-powersuite' ),
			'current'    => __( 'Current alt', 'wp-powersuite' ),
			'suggestion' => __( 'AI suggestion', 'wp-powersuite' ),
			'status'     => __( 'Status', 'wp-powersuite' ),
			'actions'    => __( 'Actions', 'wp-powersuite' ),
		];
	}

	/** @return array<string, string> */
	protected function get_bulk_actions(): array {
		return [
			'approve'     => __( 'Approve & save', 'wp-powersuite' ),
			'reject'      => __( 'Reject', 'wp-powersuite' ),
			'regenerate'  => __( 'Regenerate', 'wp-powersuite' ),
		];
	}

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

	/** @param array<string, mixed> $item */
	protected function column_image( $item ): string {
		$id       = (int) ( $item['attachment_id'] ?? 0 );
		$edit_url = get_edit_post_link( $id, 'raw' ) ?: '#';
		$thumb    = wp_get_attachment_image( $id, [ 60, 60 ] );
		$actions  = [
			'edit' => sprintf( '<a href="%s">%s</a>', esc_url( $edit_url ), esc_html__( 'Edit', 'wp-powersuite' ) ),
		];

		return ( $thumb ? $thumb : '' ) . '<div><strong><a href="' . esc_url( $edit_url ) . '">#' . esc_html( (string) $id ) . '</a></strong></div>' . $this->row_actions( $actions );
	}

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

	/** @param array<string, mixed> $item */
	protected function column_suggestion( $item ): string {
		$id         = (int) ( $item['attachment_id'] ?? 0 );
		$suggestion = (string) ( $item['suggestion'] ?? '' );
		return sprintf(
			'<textarea class="large-text wpps-ai-alt-review-field" rows="2" name="wpps_ai_alt_suggestion[%1$d]" data-attachment-id="%1$d">%2$s</textarea>',
			$id,
			esc_textarea( $suggestion )
		);
	}

	/** @param array<string, mixed> $item */
	protected function column_actions( $item ): string {
		$id = (int) ( $item['attachment_id'] ?? 0 );
		if ( $id <= 0 ) {
			return '';
		}

		return sprintf(
			'<div class="wpps-ai-alt-review-row-actions">'
			. '<button type="button" class="button button-primary button-small wpps-ai-alt-approve" data-attachment-id="%1$d">%2$s</button>'
			. '<button type="button" class="button button-small wpps-ai-alt-reject" data-attachment-id="%1$d">%3$s</button>'
			. '<button type="button" class="button button-small wpps-ai-alt-review-regenerate" data-attachment-id="%1$d">%4$s</button>'
			. '</div>',
			$id,
			esc_html__( 'Approve', 'wp-powersuite' ),
			esc_html__( 'Reject', 'wp-powersuite' ),
			esc_html__( 'Regenerate', 'wp-powersuite' )
		);
	}

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

	/** @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    = Scanner::countPendingReview( $search );
		$rows     = Scanner::getPendingReviewRows( $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 [];
	}
}
