<?php
$key = $_POST['key'];
$fileName = $_POST['fileName'];
$fileName = substr($fileName, -9);
$key = substr($key, -9);

cleanUnread($key, $fileName);

function cleanUnread($key, $fileName)
{
    $filename = 'texts/' . $fileName . '.json';

    // Check if the file exists
    if (file_exists($filename)) {
        // File exists, read the existing JSON data
        $json = json_decode(file_get_contents($filename), true);

        // Check if the key and messages properties exist in the JSON
        if (isset($json[$key]) && isset($json[$key]['messages'])) {
            // Set the 'unreadMessages' count to 0
            $json[$key]['unreadMessages'] = 0;
            // Write the updated JSON back to the file
            file_put_contents($filename, json_encode($json));
        }
    }
}
?>
