<?php

namespace SplitPay\Integrations\FluentCart;

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

if ( ! class_exists( 'FluentCart\App\Modules\Integrations\BaseIntegrationManager' ) ) {
	return;
}

use FluentCart\App\Modules\Integrations\BaseIntegrationManager;
use SplitPay\Core\ConnectedAccounts;
use SplitPay\Core\UpgradeCTA;

class SplitPayFCIntegrationManager extends BaseIntegrationManager {

	public function __construct() {
		parent::__construct(
			'Split Pay - Stripe Connect',
			'split_pay_connect',
			20
		);
		if ( UpgradeCTA::isPremium() ) {
			$this->description = 'Configure split payment transfers to connected Stripe accounts for this product.';
		} else {
			$this->description = 'Product-level split pay configuration requires PRO. Global transfers still apply to all products.';
		}
		$this->logo = '';
		$this->category = 'payment';
		$this->scopes = ['product'];
		$this->disableGlobalSettings = true;
		$this->runOnBackgroundForProduct = false;
	}

	public function isConfigured() {
		return UpgradeCTA::isPremium();
	}

	public function getApiSettings() {
		return ['status' => UpgradeCTA::isPremium()];
	}

	public function processAction($order, $eventData) {
	}

	public function validateFeedData($data, $args) {
		if ( ! empty( $data['transfer_type'] ) ) {
			if ( $data['transfer_type'] === 'percentage' ) {
				$data['transfer_amount'] = '';
			} elseif ( $data['transfer_type'] === 'amount' ) {
				$data['transfer_percentage'] = '';
			}
		}

		if ( ! empty( $data['shipping_transfer_type'] ) ) {
			if ( $data['shipping_transfer_type'] === 'percentage' ) {
				$data['shipping_transfer_amount'] = '';
			} elseif ( $data['shipping_transfer_type'] === 'amount' ) {
				$data['shipping_transfer_percentage'] = '';
			} elseif ( $data['shipping_transfer_type'] === 'none' ) {
				$data['shipping_transfer_percentage'] = '';
				$data['shipping_transfer_amount']     = '';
			}
		}

		return $data;
	}

	public function getIntegrationDefaults($settings, $args = []) {
		return [
			'name' => 'Split Pay Transfer',
			'connected_account' => '',
			'transfer_type' => 'percentage',
			'transfer_percentage' => '',
			'transfer_amount' => '',
			'shipping_transfer_type' => 'none',
			'shipping_transfer_percentage' => '',
			'shipping_transfer_amount' => '',
			'enabled' => false,
			'conditionals' => [],
		];
	}

	public function getSettingsFields($settings, $args = []) {
		$accounts = new ConnectedAccounts();
		$stored = $accounts->getStoredAccounts();

		$accountOptions = [];
		$accountOptions[''] = '-- Select Account --';
		foreach ($stored as $acct) {
			$label = '';
			if (!empty($acct['bsd_account_name'])) {
				$label = $acct['bsd_account_name'];
				if (!empty($acct['bsd_account_email'])) {
					$label .= ' - ' . $acct['bsd_account_email'];
				}
			} else {
				$label = $acct['bsd_account_id'];
			}
			$accountOptions[trim($acct['bsd_account_id'])] = $label;
		}

		return [
			'fields' => [
				[
					'key' => 'name',
					'label' => 'Feed Title',
					'required' => true,
					'placeholder' => 'e.g. Vendor Transfer',
					'component' => 'text',
					'inline_tip' => 'A name to identify this transfer configuration.',
				],
				[
					'key' => 'connected_account',
					'label' => 'Connected Stripe Account',
					'required' => true,
					'component' => 'select',
					'options' => $accountOptions,
					'inline_tip' => 'The Stripe connected account to receive the transfer.',
				],
				[
					'key' => 'transfer_type',
					'label' => 'Transfer Type',
					'required' => true,
					'component' => 'select',
					'options' => [
						'percentage' => 'Percentage',
						'amount' => 'Fixed Amount',
					],
					'inline_tip' => 'Choose whether to transfer a percentage or fixed amount per sale.',
				],
				[
					'key' => 'transfer_percentage',
					'label' => 'Transfer Percentage (%)',
					'component' => 'text',
					'placeholder' => 'e.g. 25',
					'inline_tip' => 'Percentage of the product price to transfer.',
				],
				[
					'key' => 'transfer_amount',
					'label' => 'Transfer Amount',
					'component' => 'text',
					'placeholder' => 'e.g. 10.00',
					'inline_tip' => 'Fixed amount to transfer per unit sold.',
				],
				[
					'key' => 'shipping_transfer_type',
					'label' => 'Shipping Transfer Type',
					'component' => 'select',
					'options' => [
						'none' => 'None',
						'percentage' => 'Percentage',
						'amount' => 'Fixed Amount',
					],
					'inline_tip' => 'Optionally transfer a portion of shipping fees to this account.',
				],
				[
					'key' => 'shipping_transfer_percentage',
					'label' => 'Shipping Transfer Percentage (%)',
					'component' => 'text',
					'placeholder' => 'e.g. 50',
				],
				[
					'key' => 'shipping_transfer_amount',
					'label' => 'Shipping Transfer Amount',
					'component' => 'text',
					'placeholder' => 'e.g. 5.00',
				],
			],
			'save_button_text' => 'Create Split Pay Feed',
			'button_txt'       => 'Create Split Pay Feed',
		];
	}
}
