<?php

namespace SplitPay\Integrations\FluentCart\Gateways;

use SplitPay\Contracts\GatewayAdapterInterface;
use SplitPay\Core\StripeService;

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

class FCStripeGateway implements GatewayAdapterInterface {

	public function getId(): string {
		return 'fluentcart_stripe';
	}

	public function getName(): string {
		return 'FluentCart Stripe';
	}

	public function isActive(): bool {
		return class_exists( 'FluentCart\App\Modules\PaymentMethods\StripeGateway\Stripe' );
	}

	private function getSettingsBase(): ?object {
		if ( ! class_exists( 'FluentCart\App\Modules\PaymentMethods\StripeGateway\StripeSettingsBase' ) ) {
			return null;
		}

		try {
			return new \FluentCart\App\Modules\PaymentMethods\StripeGateway\StripeSettingsBase();
		} catch ( \Throwable $e ) {
			return null;
		}
	}

	public function isTestMode(): bool {
		$ssb = $this->getSettingsBase();
		if ( ! $ssb ) {
			return false;
		}

		try {
			return $ssb->getMode() === 'test';
		} catch ( \Throwable $e ) {
			return false;
		}
	}

	public function getSecretKey(): string {
		$ssb = $this->getSettingsBase();
		if ( ! $ssb ) {
			return '';
		}

		try {
			return $ssb->getApiKey() ?: '';
		} catch ( \Throwable $e ) {
			return '';
		}
	}

	public function getPublicKey(): string {
		$ssb = $this->getSettingsBase();
		if ( ! $ssb ) {
			return '';
		}

		try {
			return $ssb->getPublicKey() ?: '';
		} catch ( \Throwable $e ) {
			return '';
		}
	}

	public function getWebhookSecret(): string {
		$ssb = $this->getSettingsBase();
		if ( ! $ssb ) {
			return '';
		}

		try {
			$settings = $ssb->getCachedSettings();
			return $settings['webhook_secret'] ?? '';
		} catch ( \Throwable $e ) {
			return '';
		}
	}

	public function resolveChargeId( $eventOrTransaction ): string {
		$piId = is_object( $eventOrTransaction )
			? ( $eventOrTransaction->vendor_charge_id ?? '' )
			: ( $eventOrTransaction['vendor_charge_id'] ?? '' );

		if ( empty( $piId ) ) {
			return '';
		}

		$secretKey = $this->getSecretKey();

		if ( empty( $secretKey ) ) {
			return '';
		}

		try {
			$stripe = new StripeService( $secretKey );
			$pi     = $stripe->retrievePaymentIntent( $piId );

			return $pi->latest_charge ?? '';
		} catch ( \Throwable $e ) {
			return '';
		}
	}

	public function getPlatformAccountId(): string {
		$secretKey = $this->getSecretKey();

		if ( empty( $secretKey ) ) {
			return '';
		}

		try {
			$stripe  = new StripeService( $secretKey );
			$account = $stripe->retrieveAccount( 'self' );

			return $account->id ?? '';
		} catch ( \Throwable $e ) {
			return '';
		}
	}

	public function supportsWebhookSync(): bool {
		return false;
	}

	public function getWebhookEndpointId(): string {
		return '';
	}

	public function getTransferSecretKey(): string {
		$isTest = $this->isTestMode();

		return $isTest
			? get_option( 'spp_fc_stripe_test_secret_key', '' )
			: get_option( 'spp_fc_stripe_live_secret_key', '' );
	}

	public function getStatusDetails(): array {
		$active  = $this->isActive();
		$testMode = $this->isTestMode();
		$hasKey  = ! empty( $this->getSecretKey() );

		return [
			'gateway_id'       => $this->getId(),
			'gateway_name'     => $this->getName(),
			'active'           => $active,
			'mode'             => $testMode ? 'test' : 'live',
			'has_secret_key'   => $hasKey,
			'has_public_key'   => ! empty( $this->getPublicKey() ),
			'has_webhook_secret' => ! empty( $this->getWebhookSecret() ),
		];
	}
}
