<?php

namespace PixelioCartAbandonRecoveryMore\Frontend\Repositories;

use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;
if (!defined('ABSPATH')) exit;
class Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository
{
	private $wpdb;
	private string $table;

	public function __construct($pixelio_wordpress_wrapper)
	{
		$this->wpdb = $pixelio_wordpress_wrapper->pixelio_get_wpdb();
		$this->table = $this->wpdb->prefix . Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_db_prefix . 'cart_item';
	}


	public function find_by_cart_and_product_id(int $cartId, int $productId): ?int
	{
		$id = $this->wpdb->get_var(
			$this->wpdb->prepare(
				"SELECT id FROM {$this->table}
				WHERE cart_id = %d AND product_id = %d LIMIT 1",
				$cartId,
				$productId
			)
		);

		return $id ? (int)$id : null;
	}

	public function insert(
		int   $cartId,
		int   $productId,
		int   $qty,
		float $price,
	): bool
	{

		return $this->wpdb->insert(
				$this->table,
				[
					'cart_id' => $cartId,
					'woo_product_id' => $productId,
					'quantity' => $qty,
					'price' => $price,
					'is_removed' => 0,
				],
				['%d', '%d', '%d', '%f', '%f', '%d']
			) !== false;
	}

	public function pixelio_remove_cart_by_product_and_cart_id(int $cartId, int $productId): bool
	{
		return $this->wpdb->update(
				$this->table,
				['is_removed' => 1],
				[
					'cart_id' => $cartId,
					'woo_product_id' => $productId
				],
				['%d'],
				['%d', '%d']
			) !== false;
	}


	public function update(
		int   $cart_id,
		int   $woo_product_id,
		int   $qty,
		int   $isRemoved = 0
	): bool
	{

		return $this->wpdb->update(
				$this->table,
				[
					'quantity'   => $qty,
					'is_removed' => $isRemoved,
					'synced'     => 0
				],
				[
					'cart_id'        => $cart_id,
					'woo_product_id' => $woo_product_id,
					'is_removed'     => 0
				],
				['%d', '%d', '%d'],   // format pour quantity, is_removed, synced
				['%d', '%d', '%d']    // format pour cart_id, woo_product_id, is_removed
			) !== false;
	}

	public function pixelio_mark_removed(int $cartId, $product_id): bool
	{
		return $this->wpdb->update(
				$this->table,
				['is_removed' => 1], // colonnes à mettre à jour
				['cart_id' => $cartId, 'woo_product_id' => $product_id], // WHERE
				['%d'],              // format pour is_removed (1 seule colonne)
				['%d', '%d']         // format pour WHERE cart_id et woo_product
			) !== false;
	}

	public function pixelio_get_not_yet_synced(): array
	{
		return $this->wpdb->get_results(
			"SELECT * FROM {$this->table} WHERE synced = 0"
		);
	}

	public function pixelio_mark_synced(array $cart_items): bool
	{
		foreach ($cart_items as $cart_item) {

			if (!isset($cart_item->cart_id)) {
				continue;
			}

			$cart_id = (int) $cart_item->cart_id;

			$result = $this->wpdb->update(
				$this->table,
				['synced' => 1],
				['cart_id' => $cart_id],
				['%d'],
				['%d']
			);

			if ($result === false) {
				return false;
			}
		}

		return true;
	}

	public function pixelio_remove_all_item_by_cart_id($cartId): bool
	{
		return $this->wpdb->update(
				$this->table,
				['is_removed' => 1],
				['cart_id' => $cartId],
				['%d'],
				['%d']
			) !== false;
	}

	public function pixelio_find_item_by_cart_id(int $cartId): array
	{
		return $this->wpdb->get_results(
			$this->wpdb->prepare(
				"SELECT * FROM {$this->table} WHERE cart_id = %d AND is_removed = 0",
				$cartId
			)
		);
	}


}
