<?php
require_once '../config.php';
require_once '../../includes/Auth.php';
require_once '../../includes/Shop.php';

$auth = new Auth();
$user = $auth->requireAuth();
$shop = new Shop();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input = json_decode(file_get_contents('php://input'), true);

    $product_id = $input['product_id'] ?? null;
    $shop_id = $input['shop_id'] ?? null;

    if (!$product_id || !$shop_id) {
        jsonResponse(['success' => false, 'message' => 'Product and Shop ID are required'], 400);
    }

    $shop_data = $shop->getShopById($shop_id);

    if (!$shop_data || ($shop_data['owner_id'] != $user['id'] && !$auth->hasRole('admin'))) {
        jsonResponse(['success' => false, 'message' => 'Unauthorized'], 403);
    }

    try {
        $shop->deleteProduct($product_id, $shop_id);
        jsonResponse(['success' => true, 'message' => 'Product deleted successfully']);
    } catch (Exception $e) {
        jsonResponse(['success' => false, 'message' => $e->getMessage()], 500);
    }
}