package main

import (
	"log"

	"github.com/veypi/vbase/internal/api"
	"github.com/veypi/vbase/internal/cache"
	"github.com/veypi/vbase/internal/config"
	"github.com/veypi/vbase/internal/model"
	"github.com/veypi/vigo"
)

func main() {
	// 初始化数据库
	if err := model.InitDB(); err != nil {
		log.Fatalf("Failed to init database: %v", err)
	}

	// 自动迁移
	if err := model.AutoMigrate(); err != nil {
		log.Fatalf("Failed to migrate database: %v", err)
	}

	// 初始化系统数据
	if err := model.InitSystemData(); err != nil {
		log.Fatalf("Failed to init system data: %v", err)
	}

	// 初始化Redis
	if err := cache.Init(); err != nil {
		log.Printf("Warning: Failed to init redis: %v", err)
	} else {
		log.Println("Redis connected")
	}

	// 创建路由
	router := api.NewRouter()

	// 创建服务器
	server, err := vigo.New(vigo.WithHost(config.C.Server.Host), vigo.WithPort(config.C.Server.Port))
	if err != nil {
		log.Fatalf("Failed to create server: %v", err)
	}
	server.Router().Extend("api", router)

	log.Printf("Server starting on %s:%d", config.C.Server.Host, config.C.Server.Port)
	if err := server.Run(); err != nil {
		log.Fatalf("Server error: %v", err)
	}
}
