<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Domain\Product;

defined('ABSPATH') || exit;

class ProductConfigBinder
{
    public const DISPLAY_MODE_META = '_apd_custom_product_display_mode';
    public const MODAL_POSITION_META = '_apd_custom_product_modal_button_position';

    public function getExactConfigId(int $postId) : int
    {
        return (int) get_post_meta($postId, '_apd_custom_product_id', true);
    }

    public function getConfigIdForProduct(int $productId, int $variationId = 0) : int
    {
        if ($variationId > 0) {
            $variationConfigId = $this->getExactConfigId($variationId);
            if ($variationConfigId > 0) {
                return $variationConfigId;
            }
        }

        return $this->getExactConfigId($productId);
    }

    public function bind(int $postId, int $configId) : void
    {
        if ($configId > 0 && get_post_type($configId) === 'apd_product_config') {
            update_post_meta($postId, '_apd_custom_product_id', $configId);
            return;
        }

        delete_post_meta($postId, '_apd_custom_product_id');
    }

    public function getVariationBindings(int $productId) : array
    {
        $variationIds = get_posts([
            'post_type' => 'product_variation',
            'post_parent' => $productId,
            'post_status' => ['publish', 'private'],
            'posts_per_page' => -1,
            'fields' => 'ids',
            'no_found_rows' => true,
        ]);

        $bindings = [];
        foreach ($variationIds as $variationId) {
            $configId = $this->getExactConfigId((int) $variationId);
            if ($configId > 0) {
                $bindings[(int) $variationId] = $configId;
            }
        }

        return $bindings;
    }

    public function productHasAnyBinding(int $productId) : bool
    {
        if ($this->getExactConfigId($productId) > 0) {
            return true;
        }

        return ! empty($this->getVariationBindings($productId));
    }

    public function getDisplayMode(int $productId) : string
    {
        $value = (string) get_post_meta($productId, self::DISPLAY_MODE_META, true);
        if (! in_array($value, ['condensed', 'full_width', 'modal'], true)) {
            return 'condensed';
        }

        return $value;
    }

    public function setDisplayMode(int $productId, string $mode) : void
    {
        if (! in_array($mode, ['condensed', 'full_width', 'modal'], true)) {
            $mode = 'condensed';
        }

        update_post_meta($productId, self::DISPLAY_MODE_META, $mode);
    }

    public function getModalButtonPosition(int $productId) : string
    {
        $value = (string) get_post_meta($productId, self::MODAL_POSITION_META, true);
        if (! in_array($value, ['above_price', 'below_price'], true)) {
            return 'below_price';
        }

        return $value;
    }

    public function setModalButtonPosition(int $productId, string $position) : void
    {
        if (! in_array($position, ['above_price', 'below_price'], true)) {
            $position = 'below_price';
        }

        update_post_meta($productId, self::MODAL_POSITION_META, $position);
    }
}
