<?php

namespace SplitPay\Integrations\WooCommerce\Adapters;

use SplitPay\Contracts\ProductAdapterInterface;

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

class WCProductAdapter implements ProductAdapterInterface {

	public function getConnectedAccounts( int $productId ): array {
		$value = get_post_meta( $productId, '_bsd_spscwt_product_connected_account', true );
		return is_array( $value ) ? $value : [];
	}

	public function getTransferType( int $productId ): array {
		$value = get_post_meta( $productId, '_bsd_spscwt_product_type', true );
		return is_array( $value ) ? $value : [];
	}

	public function getTransferPercentage( int $productId ): array {
		$value = get_post_meta( $productId, '_stripe_connect_split_pay_transfer_percentage', true );
		return is_array( $value ) ? $value : [];
	}

	public function getTransferAmount( int $productId ): array {
		$value = get_post_meta( $productId, '_bsd_spscwt_product_amount', true );
		return is_array( $value ) ? $value : [];
	}

	public function getShippingTransferSettings( int $productId ): array {
		$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 : [],
		];
	}

	public function saveProductSplitPaySettings( int $productId, array $settings ): void {
		$accounts   = $settings['connected_accounts'] ?? null;
		$types      = $settings['transfer_types'] ?? null;
		$pcts       = $settings['transfer_percentages'] ?? null;
		$amounts    = $settings['transfer_amounts'] ?? null;
		$ship_types = $settings['shipping_types'] ?? null;
		$ship_pcts  = $settings['shipping_percentages'] ?? null;
		$ship_amts  = $settings['shipping_amounts'] ?? null;

		$accounts = apply_filters( 'spp_before_save_product_accounts', $accounts, $productId );

		if ( $accounts !== null ) {
			update_post_meta( $productId, '_bsd_spscwt_product_connected_account', $accounts );
		}

		$types = apply_filters( 'spp_before_save_product_types', $types, $productId );

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

		$pcts = apply_filters( 'spp_before_save_product_transfer_percentage', $pcts, $productId );

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

		$amounts = apply_filters( 'spp_before_save_product_transfer_amount', $amounts, $productId );

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

		$ship_types = apply_filters( 'spp_before_save_product_shipping_transfer_type', $ship_types, $productId );

		if ( empty( $ship_types ) ) {
			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', $ship_types );
		}

		$ship_pcts = apply_filters( 'spp_before_save_product_shipping_transfer_percentage', $ship_pcts, $productId );

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

		$ship_amts = apply_filters( 'spp_before_save_product_shipping_transfer_amount', $ship_amts, $productId );

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