<?php

namespace Devikit\Ifirma;

use Devikit\Ifirma\Admin\Settings;
use Devikit\Ifirma\Admin\OrderColumns;
use Devikit\Ifirma\Admin\ProductFieldsPreview;
use Devikit\Ifirma\Api\Client;
use Devikit\Ifirma\Frontend\Checkout;
use Devikit\Ifirma\Frontend\CheckoutBlocks;
use Devikit\Ifirma\Frontend\MyAccount;

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

/**
 * Main Plugin class
 */
class Plugin {

	/**
	 * Singleton instance
	 *
	 * @var Plugin
	 */
	private static $instance;

	/**
	 * @var Settings
	 */
	private $settings;

	/**
	 * @var Client
	 */
	private $api_client;

	/**
	 * @var InvoiceManager
	 */
	private $invoice_manager;

	/**
	 * @var Checkout
	 */
	private $checkout;

	/**
	 * @var CheckoutBlocks
	 */
	private $checkout_blocks;

	/**
	 * @var OrderColumns
	 */
	private $order_columns;

	/**
	 * @var NipField
	 */
	private $nip_field;

	/**
	 * @var ProductFieldsPreview
	 */
	private $product_fields_preview;

	/**
	 * @var LumpSumProductFields
	 */
	private $lump_sum_product_fields;

	/**
	 * @var MyAccount
	 */
	private $my_account;

	/**
	 * Get singleton instance
	 *
	 * @return Plugin
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor
	 */
	private function __construct() {
		$this->init_hooks();
		$this->init_components();
	}

	/**
	 * Initialize hooks
	 */
	private function init_hooks() {
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
		add_filter( 'plugin_action_links_' . DEVIKIT_IFIRMA_BASENAME, [ $this, 'add_plugin_action_links' ] );
	}

	/**
	 * Initialize components
	 */
	private function init_components() {
		$this->settings = new Settings();
		
		// Get API credentials
		$api_login = $this->settings->get_option( 'api_login' );
		$api_key_abonent = $this->settings->get_option( 'api_key_abonent' );
		$api_key_faktura = $this->settings->get_option( 'api_key_faktura' );
		$api_key_rachunek = $this->settings->get_option( 'api_key_rachunek' );
		
		$this->api_client = new Client( $api_login, $api_key_abonent, $api_key_faktura, $api_key_rachunek );

		$this->invoice_manager = new InvoiceManager( $this->api_client, $this->settings );
		$this->checkout = new Checkout();
		$this->checkout_blocks = new CheckoutBlocks();
		$this->my_account = new MyAccount();
		$this->order_columns = new OrderColumns();
		$this->nip_field = new NipField();
		$this->product_fields_preview = new ProductFieldsPreview();
		$this->lump_sum_product_fields = new LumpSumProductFields();
	}

	/**
	 * Enqueue assets
	 */
	public function enqueue_assets() {
		// Enqueue styles/scripts if needed
	}

	/**
	 * Add plugin action links
	 */
	public function add_plugin_action_links( $links ) {
		$settings_link = '<a href="' . admin_url( 'admin.php?page=devikit-ifirma' ) . '">' . __( 'Settings', 'invoicing-integration-for-ifirma-and-woocommerce' ) . '</a>';
		array_unshift( $links, $settings_link );
		return $links;
	}

	/**
	 * Get Settings instance
	 * 
	 * @return Settings
	 */
	public function get_settings() {
		return $this->settings;
	}

	/**
	 * Get API Client instance
	 * 
	 * @return Client
	 */
	public function get_api_client() {
		return $this->api_client;
	}

	/**
	 * Get Invoice Manager instance
	 * 
	 * @return InvoiceManager
	 */
	public function get_invoice_manager() {
		return $this->invoice_manager;
	}
}
