package main

import (
	"os"
	"path/filepath"

	"nearle/db"
	"nearle/internal/backup"
	"nearle/utils"

	"github.com/joho/godotenv"
)

func main() {

	// 👇 Load .env
	if err := godotenv.Load(".env"); err != nil {
		utils.Logger.Fatalf("❌ Failed to load .env file: %v", err)
	}

	utils.Logger.Infof("DB_USER = %s", os.Getenv("DB_USER"))
	utils.Logger.Infof("DB_NAME = %s", os.Getenv("DB_NAME"))

	// 🔌 Connect DB
	db.Connect()

	sqlDB, err := db.DB.DB()
	if err != nil {
		utils.Logger.Fatal(err)
	}

	// ================================
	// 1️⃣ deliverylogs
	// ================================
	utils.Info("▶ Backing up deliverylogs...")
	deliveryLogsFile, err := backup.BackupDeliveryLogs(sqlDB)
	if err != nil {
		utils.Logger.Fatalf("❌ deliverylogs backup failed: %v", err)
	}

	utils.Info("☁️ Uploading deliverylogs parquet...")
	url, err := backup.UploadParquetToSpaces(
		deliveryLogsFile,
		"parquet/deliverylogs/"+filepath.Base(deliveryLogsFile),
	)
	if err != nil {
		utils.Logger.Fatalf("❌ deliverylogs upload failed: %v", err)
	}
	utils.Logger.Infof("✅ Uploaded: %s", url)

	// ================================
	// 2️⃣ deliveries
	// ================================
	utils.Info("▶ Backing up deliveries...")
	deliveriesFile, err := backup.BackupDeliveries(sqlDB)
	if err != nil {
		utils.Logger.Fatalf("❌ deliveries backup failed: %v", err)
	}

	utils.Info("☁️ Uploading deliveries parquet...")
	url, err = backup.UploadParquetToSpaces(
		deliveriesFile,
		"parquet/deliveries/"+filepath.Base(deliveriesFile),
	)
	if err != nil {
		utils.Logger.Fatalf("❌ deliveries upload failed: %v", err)
	}
	utils.Logger.Infof("✅ Uploaded: %s", url)

	// ================================
	// 3️⃣ orders  ✅ NEW
	// ================================
	utils.Info("▶ Backing up orders...")
	ordersFile, err := backup.BackupOrders(sqlDB)
	if err != nil {
		utils.Logger.Fatalf("❌ orders backup failed: %v", err)
	}

	utils.Info("☁️ Uploading orders parquet...")
	url, err = backup.UploadParquetToSpaces(
		ordersFile,
		"parquet/orders/"+filepath.Base(ordersFile),
	)
	if err != nil {
		utils.Logger.Fatalf("❌ orders upload failed: %v", err)
	}
	utils.Logger.Infof("✅ Uploaded: %s", url)

	utils.Info("🎉 Parquet backup + S3 upload completed successfully")
}
