<?php

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

$db = getDB();

$deviceId =
$_GET['device_id'] ?? '';

$list = [];

$query = $db->query("

SELECT
products.*

FROM favorites

LEFT JOIN products

ON favorites.product_id =
products.id

WHERE favorites.device_id =
'$deviceId'

ORDER BY favorites.id DESC
");

while($row = $query->fetch_assoc()){

    $images =
    json_decode($row['images']);

    $list[] = [

        "id" => $row['id'],

        "category_id" =>
        $row['category_id'],

        "name_en" =>
        $row['name_en'],

        "name_bn" =>
        $row['name_bn'],

        "description_en" =>
        $row['description_en'],

        "description_bn" =>
        $row['description_bn'],

        "price" =>
        $row['price'],

        "images" => [

            BASE_URL .
            "uploads/products/" .
            $images[0]
        ]
    ];
}

echo json_encode([

    "success" => true,

    "products" => $list
]);