package main

import (
	"ai_scheduler/internal/config"
	"context"
	"flag"
	"fmt"

	"github.com/gofiber/fiber/v2/log"
)

func main() {
	configPath := flag.String("config", "./config/config.yaml", "Path to configuration file")
	onBot := flag.String("bot", "", "bot start")
	cron := flag.String("cron", "", "close")
	runJob := flag.String("runJob", "", "run single job and exit")
	flag.Parse()
	ctx := context.Background()
	bc, err := config.LoadConfig(*configPath)
	if err != nil {
		log.Fatalf("加载配置失败: %v", err)
	}
	app, cleanup, err := InitializeApp(ctx, bc, log.DefaultLogger())
	if err != nil {
		log.Fatalf("项目初始化失败: %v", err)
	}
	defer func() {
		cleanup()
	}()
	//钉钉机器人
	app.DingBotServer.Run(ctx, *onBot)
	//定时任务 - 测试环境不启用
	if *cron == "start" {
		app.Cron.Run(ctx)
	}
	// 运行指定任务并退出
	if *runJob != "" {
		app.Cron.RunOnce(ctx, *runJob)
		return
	}

	log.Fatal(app.HttpServer.Listen(fmt.Sprintf(":%d", bc.Server.Port)))
}
