// Command raven monitors an IMAP mailbox for new messages, generates replies
// using an LLM, and sends them via SMTP. See package service for the main
// orchestration logic and package config for configuration options.
package main

import (
	"context"
	"flag"
	"log/slog"
	"os"
	"runtime/debug"

	"code.chimeric.al/chimerical/raven/internal/config"
	"code.chimeric.al/chimerical/raven/internal/service"
)

func main() {
	var cfgPath = flag.String(
		"c", "", "path to configuration file (required)",
	)
	flag.Parse()

	var log = slog.New(slog.NewTextHandler(
		os.Stderr,
		&slog.HandlerOptions{
			AddSource: true,
			Level:     slog.LevelDebug,
		},
	))
	if *cfgPath == "" {
		log.Error("missing config path")
		os.Exit(1)
	}

	// Log build info.
	bi, ok := debug.ReadBuildInfo()
	if ok {
		var commit, time string
		for _, s := range bi.Settings {
			switch s.Key {
			case "vcs.revision":
				commit = s.Value
			case "vcs.time":
				time = s.Value
			}
		}
		log.Debug(
			"raven",
			slog.Group(
				"build",
				slog.String("commit", commit),
				slog.String("time", time),
			),
		)
	}

	cfg, err := config.Load(*cfgPath)
	if err != nil {
		log.Error(
			"failed to load config",
			slog.Any("error", err),
		)
		os.Exit(1)
	}

	svc, err := service.New(cfg, log)
	if err != nil {
		log.Error(
			"failed to create service",
			slog.Any("error", err),
		)
		os.Exit(1)
	}

	if err := svc.Run(context.Background()); err != nil {
		log.Error(
			"failed to run",
			slog.Any("error", err),
		)
		os.Exit(1)
	}
}
