package main

import (
	"fmt"
	"log"

	"github.com/gin-gonic/gin"

	"infogenie-backend/config"
	"infogenie-backend/internal/database"
	"infogenie-backend/internal/router"
)

func main() {
	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("配置加载失败: %v", err)
	}

	log.Printf("InfoGenie Go Backend 启动中... [环境: %s, DB: %s:%s/%s]", cfg.Env, cfg.DB.Host, cfg.DB.Port, cfg.DB.Name)

	if err := database.Init(cfg.DB); err != nil {
		log.Fatalf("数据库初始化失败: %v", err)
	}

	if err := database.AutoMigrate(); err != nil {
		log.Fatalf("数据库迁移失败: %v", err)
	}
	log.Println("数据库表自动迁移完成")

	if cfg.Env == "production" {
		gin.SetMode(gin.ReleaseMode)
	}

	r := gin.Default()
	router.Setup(r)

	addr := fmt.Sprintf("0.0.0.0:%s", cfg.Port)
	log.Printf("🚀 InfoGenie 后端服务已启动: http://localhost:%s", cfg.Port)
	if err := r.Run(addr); err != nil {
		log.Fatalf("服务启动失败: %v", err)
	}
}
