<?php

namespace KnitPayUPI\Gateways\UpiQR\TataNeu;

use Exception;
use KnitPay\Utils as KnitPayUtils;
use KnitPayUPI\Gateways\UpiQR\KnitPayUPIIntegration;

/**
 * Title: Tata Neu Business Integration
 * Copyright: 2020-2025 Knit Pay
 *
 * @author  Knit Pay
 * @version 1.9.0.0
 * @since   1.9.0.0
 */
class Integration extends KnitPayUPIIntegration {
	private $config;

	/**
	 * Construct Tata Neu Business integration.
	 *
	 * @param array $args Arguments.
	 */
	public function __construct( $args = [] ) {
		$args = wp_parse_args(
			$args,
			[
				'id'           => 'tata-neu',
				'name'         => 'UPI/QR - Tata Neu (Personal) (Beta)',
				'product_url'  => 'https://play.google.com/store/apps/details?id=com.tatadigital.tcp',
				'provider'     => 'tata-neu',
				'gateway_name' => 'Tata Neu',
			]
		);

		parent::__construct( $args );
	}
	
	/**
	 * Get config.
	 *
	 * @param int $post_id Config ID.
	 * @return Config
	 */
	public function get_config( $post_id ) {
		$config = new Config();
		$config->copy_properties( parent::get_config( $post_id ) );

		$config->access_token = $this->get_meta( $post_id, 'upi_qr_tata_neu_access_token' );
		$config->is_connected = $this->get_meta( $post_id, 'upi_qr_tata_neu_is_connected' );

		$config->hide_mobile_qr  = false;
		$config->hide_pay_button = true;

		$this->config = $config;
		return $config;
	}

	public function get_setup_settings_fields( $fields ) {
		$fields = $this->get_login_logout_fields( $fields );

		unset( $fields['transaction_id_field'] );
		unset( $fields['merchant_category_code'] );

		return $fields;
	}

	private function get_login_logout_fields( $fields ) {
		// Connection Status.
		$config_id = KnitPayUtils::get_gateway_config_id();
		if ( ! empty( $config_id ) ) {
			$config = $this->get_config( $config_id );
		}

		// Already connected, check status.
		if ( isset( $config ) && $config->is_connected ) {
			$fields = $this->get_connection_status_field( $fields );

			$fields = parent::get_setup_settings_fields( $fields );

			return $fields;
		}

		// Access Token.
		$fields[] = [
			'section'  => 'general',
			'meta_key' => '_pronamic_gateway_upi_qr_tata_neu_access_token',
			'title'    => __( 'Access Token', 'knit-pay-upi' ),
			'type'     => 'text',
			'classes'  => [ 'regular-text', 'code' ],
			'required' => true,
		];
		
		return $fields;
	}
	
	/**
	 * Save post.
	 *
	 * @param int $post_id Post ID.
	 * @return void
	 */
	public function save_post( $config_id ) {
		$config = $this->get_config( $config_id );

		// Don't do anything if already connected.
		if ( $config->is_connected ) {
			return;
		}

		try {
			$this->save_upi_details( $config_id );
		} catch ( Exception $e ) {
			$this->knit_pay_post_save_notice( $e->getMessage() );
			return;
		}
	}

	// Get Business Name and VPA.
	private function save_upi_details( $config_id ) {
		$config = $this->get_config( $config_id );

		try {
			$api         = new Client( $config );
			$upi_details = $api->get_upi_details();
		} catch ( Exception $e ) {
			$this->knit_pay_post_save_notice( $e->getMessage() );
			return;
		}
		
		update_post_meta( $config_id, '_pronamic_gateway_upi_qr_vpa', $upi_details->vpa );
		update_post_meta( $config_id, '_pronamic_gateway_upi_qr_payee_name', $upi_details->customerName );
		update_post_meta( $config_id, '_pronamic_gateway_upi_qr_tata_neu_is_connected', true );
	}

	protected function is_connected() {
		// Check connection.
		$api_client = new Client( $this->config );
		$api_client->check_connection();
	}

	public static function clear_config( $config_id ) {
		update_post_meta( $config_id, '_pronamic_gateway_upi_qr_tata_neu_access_token', '' );
		update_post_meta( $config_id, '_pronamic_gateway_upi_qr_tata_neu_is_connected', false );
	}
}
