package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"ops-assistant/config"
	"ops-assistant/internal/bot"
	"ops-assistant/internal/channel"
	"ops-assistant/internal/core/ops"
	"ops-assistant/internal/core/runbook"
	"ops-assistant/internal/feishu"
	"ops-assistant/internal/qq"
	"ops-assistant/internal/service"
	"ops-assistant/internal/web"
	"ops-assistant/models"
	"ops-assistant/version"

	"github.com/gin-gonic/gin"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func main() {
	if len(os.Args) > 1 && (os.Args[1] == "-v" || os.Args[1] == "--version" || os.Args[1] == "version") {
		fmt.Println(version.Info())
		return
	}

	log.Printf("🦞 %s", version.Info())

	cfgPath := "config.yaml"
	if len(os.Args) > 1 {
		cfgPath = os.Args[1]
	}

	cfg, err := config.LoadConfig(cfgPath)
	if err != nil {
		log.Fatalf("无法加载配置: %v", err)
	}

	db, err := gorm.Open(sqlite.Open(cfg.Database.Path), &gorm.Config{})
	if err != nil {
		log.Fatalf("无法连接数据库: %v", err)
	}

	if err := models.Migrate(db); err != nil {
		log.Fatalf("数据库迁移失败: %v", err)
	}

	if err := channel.InitSecretCipher(cfg.Server.Key); err != nil {
		log.Fatalf("初始化渠道密钥加密失败: %v", err)
	}

	if config.IsWeakPassword(cfg.Admin.Password) {
		log.Printf("⚠️ admin 密码过弱或为默认值，请尽快修改")
	}

	// DB 渠道配置覆盖 YAML 配置
	if err := channel.ApplyChannelConfig(db, cfg); err != nil {
		log.Printf("⚠️ 渠道配置加载失败，继续使用 YAML: %v", err)
	}

	finance := service.NewFinanceService(db)
	defer finance.Close()

	if err := runbook.SeedDefaultTargets(db); err != nil {
		log.Printf("⚠️ 初始化ops targets失败: %v", err)
	}

	opsSvc := ops.BuildDefault(db, cfg.Database.Path, ".")

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	if cfg.Telegram.Enabled {
		tgBot, err := bot.NewTGBot(db, cfg.Telegram.Token, finance, opsSvc)
		if err != nil {
			log.Printf("⚠️ TG Bot 启动失败: %v", err)
		} else {
			go tgBot.Start(ctx)
		}
	}

	if cfg.QQBot.Enabled {
		qqBot := qq.NewQQBot(db, cfg.QQBot.AppID, cfg.QQBot.Secret, finance, opsSvc)
		go qqBot.Start(ctx)
	}

	engine := gin.New()
	engine.Use(gin.Recovery())
	engine.Use(gin.Logger())

	reloadFn := func() (string, error) {
		if err := channel.ApplyChannelConfig(db, cfg); err != nil {
			return "", err
		}
		return fmt.Sprintf("reload ok: tg=%v qq=%v feishu=%v", cfg.Telegram.Enabled, cfg.QQBot.Enabled, cfg.Feishu.Enabled), nil
	}

	webServer := web.NewWebServer(db, cfg.Database.Path, ".", finance, cfg.Server.Port, cfg.Admin.Username, cfg.Admin.Password, cfg.Server.Key, reloadFn)
	webServer.RegisterRoutes(engine)

	if cfg.Feishu.Enabled {
		fsBot := feishu.NewBot(db, finance, opsSvc, cfg.Feishu.AppID, cfg.Feishu.AppSecret, cfg.Feishu.VerificationToken, cfg.Feishu.EncryptKey)
		fsBot.RegisterRoutes(engine)
		go fsBot.Start(ctx)
	}

	go func() {
		logAddr := fmt.Sprintf(":%d", cfg.Server.Port)
		log.Printf("🌐 Web后台运行在 http://127.0.0.1%s", logAddr)
		if err := engine.Run(logAddr); err != nil {
			log.Printf("❌ Web服务启动失败: %v", err)
		}
	}()

	log.Println("🛠️ Ops-Assistant 已全面启动")
	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	<-sig

	log.Println("⏳ 正在关闭服务...")
	cancel()
	time.Sleep(2 * time.Second)

	sqlDB, err := db.DB()
	if err == nil {
		sqlDB.Close()
	}

	log.Println("👋 Ops-Assistant 已关闭")
}
