<?php

namespace SplitPay\Integrations\FluentCart\Adapters;

use SplitPay\Contracts\ProductAdapterInterface;

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

class FCProductAdapter implements ProductAdapterInterface {

	public function getConnectedAccounts( int $productId ): array {
		$integration = $this->getIntegrationSettings( $productId );
		if ( $integration !== null && ! empty( $integration['connected_account'] ) ) {
			return [ 0 => $integration['connected_account'] ];
		}

		$accounts = get_post_meta( $productId, '_bsd_spscwt_product_connected_account', true );

		return is_array( $accounts ) ? $accounts : [];
	}

	public function getTransferType( int $productId ): array {
		$integration = $this->getIntegrationSettings( $productId );
		if ( $integration !== null && ! empty( $integration['transfer_type'] ) ) {
			return [ 0 => $integration['transfer_type'] ];
		}

		$types = get_post_meta( $productId, '_bsd_spscwt_product_type', true );

		return is_array( $types ) ? $types : [];
	}

	public function getTransferPercentage( int $productId ): array {
		$integration = $this->getIntegrationSettings( $productId );
		if ( $integration !== null && isset( $integration['transfer_percentage'] ) && $integration['transfer_percentage'] !== '' ) {
			return [ 0 => $integration['transfer_percentage'] ];
		}

		$percentages = get_post_meta( $productId, '_stripe_connect_split_pay_transfer_percentage', true );

		return is_array( $percentages ) ? $percentages : [];
	}

	public function getTransferAmount( int $productId ): array {
		$integration = $this->getIntegrationSettings( $productId );
		if ( $integration !== null && isset( $integration['transfer_amount'] ) && $integration['transfer_amount'] !== '' ) {
			return [ 0 => $integration['transfer_amount'] ];
		}

		$amounts = get_post_meta( $productId, '_bsd_spscwt_product_amount', true );

		return is_array( $amounts ) ? $amounts : [];
	}

	public function getShippingTransferSettings( int $productId ): array {
		$integration = $this->getIntegrationSettings( $productId );
		if ( $integration !== null && ! empty( $integration['shipping_transfer_type'] ) && $integration['shipping_transfer_type'] !== 'none' ) {
			return [
				'types'       => [ 0 => $integration['shipping_transfer_type'] ],
				'percentages' => [ 0 => $integration['shipping_transfer_percentage'] ?? '' ],
				'amounts'     => [ 0 => $integration['shipping_transfer_amount'] ?? '' ],
			];
		}

		$types       = get_post_meta( $productId, '_bsd_spscwt_shipping_type', true );
		$percentages = get_post_meta( $productId, '_bsd_prod_shipping_percentage', true );
		$amounts     = get_post_meta( $productId, '_bsd_spscwt_shipping_amount', true );

		return [
			'types'       => is_array( $types ) ? $types : [],
			'percentages' => is_array( $percentages ) ? $percentages : [],
			'amounts'     => is_array( $amounts ) ? $amounts : [],
		];
	}

	private function getIntegrationSettings( int $productId ): ?array {
		global $wpdb;
		$table = $wpdb->prefix . 'fct_product_meta';

		if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table}'" ) !== $table ) {
			return null;
		}

		$row = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT meta_value FROM {$table} WHERE object_id = %d AND object_type = 'product_integration' AND meta_key = 'split_pay_connect' LIMIT 1",
				$productId
			)
		);

		if ( empty( $row ) ) {
			return null;
		}

		$data = json_decode( $row, true );
		if ( ! is_array( $data ) || empty( $data['connected_account'] ) ) {
			return null;
		}

		return $data;
	}

	public function saveProductSplitPaySettings( int $productId, array $settings ): void {
		$accounts = apply_filters(
			'spp_before_save_product_accounts',
			$settings['connected_accounts'] ?? [],
			$productId
		);

		if ( empty( $accounts ) ) {
			delete_post_meta( $productId, '_bsd_spscwt_product_connected_account' );
		} else {
			update_post_meta( $productId, '_bsd_spscwt_product_connected_account', $accounts );
		}

		$types = apply_filters(
			'spp_before_save_product_types',
			$settings['transfer_types'] ?? [],
			$productId
		);

		if ( empty( $types ) ) {
			delete_post_meta( $productId, '_bsd_spscwt_product_type' );
		} else {
			update_post_meta( $productId, '_bsd_spscwt_product_type', $types );
		}

		$percentages = apply_filters(
			'spp_before_save_product_transfer_percentage',
			$settings['transfer_percentages'] ?? null,
			$productId
		);

		if ( $percentages === null ) {
			delete_post_meta( $productId, '_stripe_connect_split_pay_transfer_percentage' );
		} else {
			update_post_meta( $productId, '_stripe_connect_split_pay_transfer_percentage', $percentages );
		}

		$amounts = apply_filters(
			'spp_before_save_product_transfer_amount',
			$settings['transfer_amounts'] ?? [],
			$productId
		);

		if ( empty( $amounts ) ) {
			delete_post_meta( $productId, '_bsd_spscwt_product_amount' );
		} else {
			update_post_meta( $productId, '_bsd_spscwt_product_amount', $amounts );
		}

		$shippingTypes = apply_filters(
			'spp_before_save_product_shipping_transfer_type',
			$settings['shipping_types'] ?? [],
			$productId
		);

		if ( empty( $shippingTypes ) ) {
			delete_post_meta( $productId, '_bsd_spscwt_shipping_type' );
			delete_post_meta( $productId, '_bsd_prod_shipping_percentage' );
			delete_post_meta( $productId, '_bsd_spscwt_shipping_amount' );
		} else {
			update_post_meta( $productId, '_bsd_spscwt_shipping_type', $shippingTypes );
		}

		$shippingPercentages = apply_filters(
			'spp_before_save_product_shipping_transfer_percentage',
			$settings['shipping_percentages'] ?? null,
			$productId
		);

		if ( $shippingPercentages === null ) {
			delete_post_meta( $productId, '_bsd_prod_shipping_percentage' );
		} else {
			update_post_meta( $productId, '_bsd_prod_shipping_percentage', $shippingPercentages );
		}

		$shippingAmounts = apply_filters(
			'spp_before_save_product_shipping_transfer_amount',
			$settings['shipping_amounts'] ?? [],
			$productId
		);

		if ( empty( $shippingAmounts ) ) {
			delete_post_meta( $productId, '_bsd_spscwt_shipping_amount' );
		} else {
			update_post_meta( $productId, '_bsd_spscwt_shipping_amount', $shippingAmounts );
		}
	}
}
