package main

import (
	"log"
	"os"

	"legalgo-BE-go/config"
	"legalgo-BE-go/database"
	categorydomain "legalgo-BE-go/internal/domain/category"
	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 tempSP subscribeplandomain.SubscribePlan
	var tempCtg categorydomain.Category

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

	if err := db.Where("code = ?", "spotlight").First(&tempCtg).Error; err != nil {
		log.Print("seeding basic subscribe plan")
		seq := 0
		db.Create(&categorydomain.Category{
			ID:       uuid.NewString(),
			Code:     "spotlight",
			Name:     "Spotlight",
			Sequence: &seq,
		})
	}

	log.Print("migrate success")
}
