package main

import (
	"log"
	"os"

	"legalgo-BE-go/config"
	"legalgo-BE-go/database"
	subscribeplandomain "legalgo-BE-go/internal/domain/subscribe_plan"

	"github.com/google/uuid"
	"github.com/joho/godotenv"
)

func main() {
	// Check if the .env file exists
	if _, err := os.Stat(".env"); err == nil {
		// Load environment variables from .env file if it exists
		if err := godotenv.Load(); err != nil {
			log.Println("Error loading .env file, continuing without it")
		}
	} else {
		log.Println(".env file not found, skipping load")
	}

	config.InitEnv()
	cfg := config.LoadConfig()
	db, err := database.NewDB(cfg)
	if err != nil {
		log.Fatalf("failed to connect to database: %v", err)
	}

	// db.DropTables()

	if err := db.Migrate(); err != nil {
		log.Fatal("Migration failed: ", err)
	}

	var temp subscribeplandomain.SubscribePlan

	if err := db.Where("code = ?", "basic").First(&temp).Error; err != nil {
		log.Print("seeding basic subscribe plan")
		db.Create(&subscribeplandomain.SubscribePlan{
			ID:   uuid.NewString(),
			Code: "basic",
			Name: "Basic",
		})
	}

	log.Print("migrate success")
}
