import 'dart:io';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/api_consts.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';

class TranslationService {
// Replace with your actual hosting URL
  static final String baseUrl = ApiConsts.googleCloudStorageENTranslationFileBaseURL;

  static Future<void> downloadTranslations() async {
    final directory = await getApplicationDocumentsDirectory();

// Ensure these match your locale codes exactly (en-US, ar-SA)
    List<String> files = ["en-US.json", "ar-SA.json"];

    for (String fileName in files) {
      try {
        final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();
        String url = "$baseUrl/$fileName?v=$timestamp";
        debugPrint(url);
        // final response = await http.get(Uri.parse("$baseUrl/$fileName"));
        final response = await http.get(Uri.parse(url));

        if (response.statusCode == 200) {
          final file = File("${directory.path}/$fileName");
          await file.writeAsBytes(response.bodyBytes);
        } else {
          print("Failed to download $fileName: Status ${response.statusCode}");
        }
      } catch (e) {
        print("Network error while downloading $fileName: $e");
      }
    }
  }
}
