<?php
/**
 * Shared Stats List Table Actions
 *
 * @package AmbassadorHub\Admin
 */

namespace AmbassadorHub\Admin;

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

use function add_filter;
use function admin_url;
use function esc_url;
use function get_current_screen;
use function get_post;
use function in_array;
use function __;

/**
 * Central helper for stats-aware list-table title-link and row-action behaviour.
 */
final class StatsListTableActions {

	/**
	 * Register shared list-table actions for a post type.
	 *
	 * @param string $post_type         Post type slug.
	 * @param string $stats_page_slug   Admin page slug for stats/detail view.
	 * @param string $row_actions_hook  Row actions filter hook.
	 * @param array  $active_statuses   Post statuses that get stats links. Empty array means all statuses.
	 *
	 * @return void
	 */
	public static function init(
		string $post_type,
		string $stats_page_slug,
		string $row_actions_hook = 'post_row_actions',
		array $active_statuses = array( 'publish', 'ended' )
	): void {
		add_filter(
			'get_edit_post_link',
			static function ( ?string $link, int $post_id ) use ( $post_type, $stats_page_slug, $active_statuses ): ?string {
				return self::title_link_to_stats( $link, $post_id, $post_type, $stats_page_slug, $active_statuses );
			},
			10,
			2
		);

		add_filter(
			$row_actions_hook,
			static function ( array $actions, \WP_Post $post ) use ( $post_type, $stats_page_slug, $active_statuses ): array {
				return self::row_actions( $actions, $post, $post_type, $stats_page_slug, $active_statuses );
			},
			10,
			2
		);
	}

	/**
	 * Rewrite title links to the module detail/stats page on list screens.
	 *
	 * @param string|null $link            Existing edit link.
	 * @param int         $post_id         Post ID.
	 * @param string      $post_type       Post type slug.
	 * @param string      $stats_page_slug Admin page slug for stats/detail view.
	 * @param array       $active_statuses Allowed post statuses. Empty array means all statuses.
	 *
	 * @return string|null
	 */
	private static function title_link_to_stats( ?string $link, int $post_id, string $post_type, string $stats_page_slug, array $active_statuses ): ?string {
		$post = get_post( $post_id );
		if ( ! $post || $post_type !== $post->post_type ) {
			return $link;
		}

		if ( ! empty( $active_statuses ) && ! in_array( $post->post_status, $active_statuses, true ) ) {
			return $link;
		}

		if ( ! function_exists( 'get_current_screen' ) ) {
			return $link;
		}

		$screen = get_current_screen();
		if ( ! $screen || 'edit' !== $screen->base ) {
			return $link;
		}

		return admin_url( 'admin.php?page=' . $stats_page_slug . '&post_id=' . $post_id );
	}

	/**
	 * Replace row actions with shared stats/edit behaviour.
	 *
	 * @param array    $actions         Existing row actions.
	 * @param \WP_Post $post            Current post object.
	 * @param string   $post_type       Post type slug.
	 * @param string   $stats_page_slug Admin page slug for stats/detail view.
	 * @param array    $active_statuses Allowed post statuses. Empty array means all statuses.
	 *
	 * @return array
	 */
	private static function row_actions( array $actions, \WP_Post $post, string $post_type, string $stats_page_slug, array $active_statuses ): array {
		if ( $post->post_type !== $post_type ) {
			return $actions;
		}

		// Remove quick edit — not applicable here.
		unset( $actions['inline hide-if-no-js'] );

		// Restore a real edit link (the title now goes to the detail page).
		$edit_url        = admin_url( 'post.php?post=' . $post->ID . '&action=edit' );
		$actions['edit'] = '<a href="' . esc_url( $edit_url ) . '">' . __( 'Edit', 'ambassadorhub' ) . '</a>';

		if ( empty( $active_statuses ) || in_array( $post->post_status, $active_statuses, true ) ) {
			// Add a Stats / Detail quick-link as the first action.
			$stats_url = admin_url( 'admin.php?page=' . $stats_page_slug . '&post_id=' . $post->ID );
			$actions   = array( 'stats' => '<a href="' . esc_url( $stats_url ) . '">' . __( 'Stats', 'ambassadorhub' ) . '</a>' ) + $actions;
		}

		return $actions;
	}
}
