<?php

namespace VendorTasksDokanClickUp\Admin;

use VendorTasksDokanClickUp\VendorIntegration\VendorTaskState;

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

/**
 * Vendor Profile Admin Integration
 *
 * Adds a ClickUp task button to the user edit screen and a bulk action
 * on the Users list for manual/backfill task creation.
 *
 * @since 1.1.0
 * @package VendorTasksDokanClickUp
 * @subpackage Admin
 */
class VendorProfile {

	/**
	 * Class constructor
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		add_action( 'show_user_profile', array( $this, 'render_task_section' ) );
		add_action( 'edit_user_profile', array( $this, 'render_task_section' ) );

		add_filter( 'bulk_actions-users', array( $this, 'register_bulk_action' ) );
		add_filter( 'handle_bulk_action-users', array( $this, 'handle_bulk_action' ), 10, 3 );
		add_action( 'admin_notices', array( $this, 'bulk_action_notice' ) );
	}

	/**
	 * Render ClickUp task section on the user/vendor edit profile page
	 *
	 * @since 1.1.0
	 *
	 * @param \WP_User $user User object.
	 * @return void
	 */
	public function render_task_section( \WP_User $user ): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$state    = new VendorTaskState();
		$task_id  = $state->get_task_id( $user->ID );
		$task_url = $state->get_task_url( $user->ID );
		$created  = $state->has_task( $user->ID );

		wc_get_template(
			'admin/vendor-clickup-section.php',
			array(
				'user'     => $user,
				'task_id'  => $task_id,
				'task_url' => $task_url,
				'created'  => $created,
				'rest_url' => rest_url( 'vendor-tasks-dokan-clickup/v1/create-task' ),
				'nonce'    => wp_create_nonce( 'wp_rest' ),
			),
			'vendor-tasks-dokan-clickup/',
			VENDOR_TASKS_DOKAN_CLICKUP_PATH . 'templates/'
		);
	}

	/**
	 * Add "Create ClickUp Tasks" bulk action to Users list
	 *
	 * @since 1.1.0
	 *
	 * @param array $bulk_actions Existing bulk actions.
	 * @return array
	 */
	public function register_bulk_action( array $bulk_actions ): array {
		$bulk_actions['dokan_clickup_create_tasks'] = __( 'Create ClickUp Tasks', 'alamin-vendor-tasks-dokan-clickup' );
		return $bulk_actions;
	}

	/**
	 * Handle "Create ClickUp Tasks" bulk action
	 *
	 * @since 1.1.0
	 *
	 * @param string $redirect_url Redirect URL.
	 * @param string $action       Action name.
	 * @param int[]  $user_ids     Selected user IDs.
	 * @return string Updated redirect URL.
	 */
	public function handle_bulk_action( string $redirect_url, string $action, array $user_ids ): string {
		if ( 'dokan_clickup_create_tasks' !== $action ) {
			return $redirect_url;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return $redirect_url;
		}

		$queued = 0;
		$queue  = new \VendorTasksDokanClickUp\Queue\PendingQueue();

		foreach ( $user_ids as $user_id ) {
			$vendor_id = absint( $user_id );
			if ( ! $vendor_id ) {
				continue;
			}
			( new VendorTaskState() )->clear_task( $vendor_id );
			$queue->enqueue( $vendor_id );
			++$queued;
		}

		vendor_tasks_dokan_clickup()->log(
			sprintf( 'Bulk action: queued %d vendor(s) for ClickUp task creation.', $queued ),
			'info'
		);

		return add_query_arg(
			array(
				'dokan_clickup_bulk_queued' => $queued,
			),
			$redirect_url
		);
	}

	/**
	 * Show admin notice after bulk action
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	public function bulk_action_notice(): void {
		$screen = get_current_screen();
		if ( ! $screen || 'users' !== $screen->id ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$queued = isset( $_GET['dokan_clickup_bulk_queued'] ) ? absint( $_GET['dokan_clickup_bulk_queued'] ) : 0;
		if ( ! $queued ) {
			return;
		}

		printf(
			'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
			esc_html(
				sprintf(
					/* translators: %d: number of vendors queued */
					_n(
						'%d vendor queued for ClickUp task creation (runs every 15 min via WP-Cron).',
						'%d vendors queued for ClickUp task creation (runs every 15 min via WP-Cron).',
						$queued,
						'alamin-vendor-tasks-dokan-clickup'
					),
					$queued
				)
			)
		);
	}
}
