<?php

require_once BASE_PATH
    . '/config/database.php';


class CartItem
{
    private PDO $db;


    public function __construct()
    {
        $database =
            new Database();

        $this->db =
            $database->connect();
    }


    public function getItems(
        int $cartId
    ): array {

        $stmt =
            $this->db->prepare("
                SELECT
                    ci.product_id,
                    ci.quantity,

                    p.id,
                    p.sku,
                    p.name,
                    p.slug,
                    p.price,
                    p.sale_price,
                    p.stock_quantity,
                    p.featured_image,
                    p.status,

                    CASE
                        WHEN p.sale_price IS NOT NULL
                             AND p.sale_price < p.price
                        THEN p.sale_price
                        ELSE p.price
                    END AS current_price

                FROM cart_items ci

                INNER JOIN products p
                    ON p.id = ci.product_id

                WHERE ci.cart_id = :cart_id

                ORDER BY ci.id ASC
            ");

        $stmt->execute([
            'cart_id' => $cartId
        ]);

        return $stmt->fetchAll();
    }


    public function getQuantity(
        int $cartId,
        int $productId
    ): int {

        $stmt =
            $this->db->prepare("
                SELECT quantity

                FROM cart_items

                WHERE cart_id = :cart_id
                  AND product_id = :product_id

                LIMIT 1
            ");

        $stmt->execute([
            'cart_id' => $cartId,
            'product_id' => $productId
        ]);

        return (int)
            $stmt->fetchColumn();
    }


    public function setQuantity(
        int $cartId,
        int $productId,
        int $quantity
    ): void {

        $quantity = max(1, $quantity);

        $stmt =
            $this->db->prepare("
                INSERT INTO cart_items (
                    cart_id,
                    product_id,
                    quantity
                )
                VALUES (
                    :cart_id,
                    :product_id,
                    :quantity
                )
                ON DUPLICATE KEY UPDATE
                    quantity = VALUES(quantity),
                    updated_at = CURRENT_TIMESTAMP
            ");

        $stmt->execute([
            'cart_id' => $cartId,
            'product_id' => $productId,
            'quantity' => $quantity
        ]);
    }


    public function remove(
        int $cartId,
        int $productId
    ): void {

        $stmt =
            $this->db->prepare("
                DELETE FROM cart_items

                WHERE cart_id = :cart_id
                  AND product_id = :product_id
            ");

        $stmt->execute([
            'cart_id' => $cartId,
            'product_id' => $productId
        ]);
    }


    public function countQuantity(
        int $cartId
    ): int {

        $stmt =
            $this->db->prepare("
                SELECT COALESCE(SUM(quantity), 0)

                FROM cart_items

                WHERE cart_id = :cart_id
            ");

        $stmt->execute([
            'cart_id' => $cartId
        ]);

        return (int)
            $stmt->fetchColumn();
    }
}
