<?php

class JsonStorageService {
    private static function getFilePath(string $type): string {
        return __DIR__ . '/../../storage/data/' . $type . '.json';
    }

    public static function read(string $type): array {
        $file = self::getFilePath($type);
        if (!file_exists($file)) {
            return [];
        }
        $json = file_get_contents($file);
        $data = json_decode($json, true);
        return is_array($data) ? $data : [];
    }

    public static function write(string $type, array $data): bool {
        $file = self::getFilePath($type);
        $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
        return file_put_contents($file, $json) !== false;
    }
}
