<?php

namespace WPDesk\FakturyKSEF\Dashboard;

use WPDesk\FakturyKSEF\Api\APIClient;
use WPDeskKSEFVendor\WPDesk\PluginBuilder\Plugin\Hookable;
use WPDeskKSEFVendor\WPDesk_Plugin_Info;

class BulkAction implements Hookable {

	private WPDesk_Plugin_Info $plugin_info;

	private const BULK_ACTION = 'send_to_ksef';

	private APIClient $api;

	public function __construct( WPDesk_Plugin_Info $plugin_info, APIClient $api ) {
		$this->api         = $api;
		$this->plugin_info = $plugin_info;
	}

	public function hooks() {
		add_filter( 'bulk_actions-edit-inspire_invoice', [ $this, 'register_bulk_action' ] );
	}

	public function register_bulk_action( $actions ) {

		$current_status = isset( $_GET['post_status'] ) ? $_GET['post_status'] : ''; //phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		if ( 'trash' === $current_status ) {
			return $actions;
		}

		if ( ! $this->api->authenticate() ) {
			return $actions;
		}

		$actions[ self::BULK_ACTION ] = esc_html__( 'Send to KSeF', 'ksef-for-flexible-invoices' );
		return $actions;
	}
}
