import * as dotenv from "dotenv";
dotenv.config({ path: ".env.local" });
import PocketBase from "pocketbase";

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

const PB_URL = process.env.NEXT_PUBLIC_PB_URL;
const ADMIN_EMAIL = process.env.PB_ADMIN_EMAIL;
const ADMIN_PASS = process.env.PB_ADMIN_PASS;

const pb = new PocketBase(PB_URL);

// --- ДАННЫЕ ДЛЯ ЗАГРУЗКИ ---
const rawDownloads = [
  // --- RU ---
  {
    lang: "ru",
    name: "Windows Клиент",
    platformId: "windows",
    version: "v1.2.4-stable",
    status: "active",
    storeLink: "https://github.com/netrunner-vpn/desktop",
    order: 1,
  },
  {
    lang: "ru",
    name: "Android APK",
    platformId: "android",
    version: "v1.1.0-release",
    status: "active",
    storeLink: "", // Оставляем пустым для теста "Скоро в Store"
    order: 2,
  },
  {
    lang: "ru",
    name: "Linux Core",
    platformId: "linux",
    version: "v1.2.4-build",
    status: "active",
    storeLink: "https://github.com/netrunner-vpn/core",
    order: 3,
  },
  {
    lang: "ru",
    name: "macOS Desktop",
    platformId: "macos",
    version: "В разработке",
    status: "dev",
    storeLink: "",
    order: 4,
  },
  {
    lang: "ru",
    name: "iOS App",
    platformId: "ios",
    version: "Бета-тест",
    status: "dev",
    storeLink: "",
    order: 5,
  },

  // --- EN ---
  {
    lang: "en",
    name: "Windows Client",
    platformId: "windows",
    version: "v1.2.4-stable",
    status: "active",
    storeLink: "https://github.com/netrunner-vpn/desktop",
    order: 1,
  },
  {
    lang: "en",
    name: "Android APK",
    platformId: "android",
    version: "v1.1.0-release",
    status: "active",
    storeLink: "",
    order: 2,
  },
  {
    lang: "en",
    name: "Linux Core",
    platformId: "linux",
    version: "v1.2.4-build",
    status: "active",
    storeLink: "https://github.com/netrunner-vpn/core",
    order: 3,
  },
  {
    lang: "en",
    name: "macOS Desktop",
    platformId: "macos",
    version: "Coming soon",
    status: "dev",
    storeLink: "",
    order: 4,
  },
  {
    lang: "en",
    name: "iOS App",
    platformId: "ios",
    version: "Under development",
    status: "dev",
    storeLink: "",
    order: 5,
  },
];

async function migrateDownloadsOnly() {
  try {
    console.log("🚀 Auth in progress...");

    const authData = await pb.send("/api/admins/auth-with-password", {
      method: "POST",
      body: { identity: ADMIN_EMAIL, password: ADMIN_PASS },
    });
    pb.authStore.save(authData.token, authData.admin);

    console.log("🔑 Access Granted. Syncing downloads...\n");

    for (let item of rawDownloads) {
      const { lang, ...cleanData } = item;
      const targetTable = `downloads_${lang}`;

      try {
        // Создаем запись
        await pb.collection(targetTable).create(cleanData);
        console.log(
          `✅ [${targetTable}] Created: ${cleanData.name} (${cleanData.platformId})`,
        );
      } catch (err) {
        console.error(
          `❌ [${targetTable}] Failed ${cleanData.name}:`,
          err.message,
        );
      }
    }

    console.log("\n🏁 All downloads synchronized.");
  } catch (err) {
    console.error("💥 Global Error:", err.message);
  }
}

migrateDownloadsOnly();
