package main

import (
	"auth-service/internal/handler"
	"auth-service/internal/model"
	"auth-service/internal/repository"
	"auth-service/internal/service"
	"auth-service/pkg/middleware"
	"auth-service/pkg/utils"
	"fmt"
	"log"
	"os"

	_ "auth-service/docs" // docs is generated by Swag CLI

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

// @title           Auth Service API
// @version         1.0
// @description     LLM Filter System Authentication Service API
// @host            localhost:8080
// @BasePath        /api/v1
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization

func main() {
	// 获取环境变量配置
	dbHost := os.Getenv("DB_HOST")
	dbUser := os.Getenv("DB_USER")
	dbPassword := os.Getenv("DB_PASSWORD")
	dbName := os.Getenv("DB_NAME")
	dbPort := os.Getenv("DB_PORT")

	// 默认值处理（用于本地开发）
	if dbHost == "" {
		dbHost = "localhost"
	}
	if dbUser == "" {
		dbUser = "admin"
	}
	if dbPassword == "" {
		dbPassword = "password" // 请确保本地有此密码的数据库或修改此处
	}
	if dbName == "" {
		dbName = "llm_filter_db"
	}
	if dbPort == "" {
		dbPort = "5433"
	}

	dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
		dbHost, dbUser, dbPassword, dbName, dbPort)

	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("Failed to connect to database: %v", err)
	}

	// 自动迁移数据库结构
	if err := db.AutoMigrate(&model.User{}, &model.Binding{}); err != nil {
		log.Fatalf("Failed to migrate database: %v", err)
	}

	// 初始化依赖注入
	userRepo := repository.NewUserRepository(db)

	// 初始化管理员账号
	initAdminUser(userRepo)

	bindingRepo := repository.NewBindingRepository(db)
	bindingSvc := service.NewBindingService(bindingRepo)
	bindingHandler := handler.NewBindingHandler(bindingSvc)

	authSvc := service.NewAuthService(userRepo, bindingRepo)
	authHandler := handler.NewAuthHandler(authSvc)

	r := gin.Default()

	// Swagger 文档路由
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// 路由分组
	v1 := r.Group("/api/v1")
	{
		authGroup := v1.Group("/auth")
		{
			authGroup.POST("/login", authHandler.Login)
			authGroup.POST("/register", authHandler.Register)
		}

		// 绑定相关路由，需要认证
		bindingsGroup := v1.Group("/bindings")
		bindingsGroup.Use(middleware.AuthMiddleware())
		{
			bindingsGroup.POST("", bindingHandler.Bind)
			bindingsGroup.DELETE("/:person_id", bindingHandler.Unbind)
			bindingsGroup.GET("/me", bindingHandler.Me)
		}
	}

	log.Println("Auth Service starting on :8081...")
	if err := r.Run(":8081"); err != nil {
		log.Fatal("Server failed to start:", err)
	}
}

func initAdminUser(repo *repository.UserRepository) {
	adminUsername := "admin"
	exists, err := repo.ExistsByUsername(adminUsername)
	if err != nil {
		log.Printf("Failed to check admin user existence: %v", err)
		return
	}

	if !exists {
		log.Println("Admin user not found. Creating default admin user...")

		adminPassword := os.Getenv("ADMIN_PASSWORD")
		if adminPassword == "" {
			adminPassword = "password123"
		}

		hashedPwd, err := utils.HashPassword(adminPassword)
		if err != nil {
			log.Printf("Failed to hash admin password: %v", err)
			return
		}

		adminUser := &model.User{
			Username:  adminUsername,
			Email:     "admin@example.com",
			Password:  hashedPwd,
			Role:      "administrator", // 兼容旧系统的最高权限角色
			RoleLevel: 5,
			Edition:   "edu",
		}

		if err := repo.Create(adminUser); err != nil {
			log.Printf("Failed to create admin user: %v", err)
		} else {
			log.Printf("Admin user created successfully. Username: %s, Password: %s", adminUsername, adminPassword)
		}
	} else {
		log.Println("Admin user already exists.")
	}
}
