<?php
include 'db.php';

$action = $_POST['action'] ?? '';
$meal_id = intval($_POST['meal_id'] ?? 0);
$material_id = intval($_POST['material_id'] ?? 0);

if(!$meal_id || !$material_id){
    exit('Invalid data');
}

if($action == 'assign'){
    $conn->query("INSERT IGNORE INTO meal_materials (meal_id, material_id) VALUES ($meal_id, $material_id)");
    echo "Material assigned to meal";
}
elseif($action == 'remove'){
    $conn->query("DELETE FROM meal_materials WHERE meal_id=$meal_id AND material_id=$material_id");
    echo "Material removed from meal";
}
else{
    echo "Invalid action";
}
