<?php
declare(strict_types=1);

require_once __DIR__ . '/../../../security/auth_guard.php';
require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../core/Response.php';

$db = Database::getInstance();
$data = json_decode(file_get_contents('php://input'), true);

if (empty($data['image_id']) || empty($data['product_id'])) {
    Response::error('image_id & product_id required', 422);
}

$db->beginTransaction();

$stmt = $db->prepare("UPDATE retail_product_images SET is_featured = 0 WHERE product_id = :pid");
$stmt->execute(['pid' => (int)$data['product_id']]);

$stmt = $db->prepare("UPDATE retail_product_images SET is_featured = 1 WHERE id = :id");
$stmt->execute(['id' => (int)$data['image_id']]);

$db->commit();

Response::success('Featured image updated');
