package main

import (
	"context"
	"os"

	"github.com/sirupsen/logrus"
	"github.com/urfave/cli/v3"

	"git.maze.io/ham/hamview"
	"git.maze.io/ham/hamview/cmd"
)

var logger = logrus.New()

func main() {
	cmd := &cli.Command{
		Name:   "hamview-server",
		Usage:  "Server for HAM radio protocols",
		Action: run,
		Before: cmd.ConfigureLogging(&logger),
		Flags:  cmd.AllFlags("hamview-server.yaml"),
	}

	if err := cmd.Run(context.Background(), os.Args); err != nil {
		logger.Fatal(err)
	}
}

type serverConfig struct {
	Database hamview.DatabaseConfig `yaml:"database"`
	Broker   hamview.BrokerConfig   `yaml:"broker"`
	Server   hamview.ServerConfig   `yaml:"server"`
	Include  []string               `yaml:"include"`
}

func (config *serverConfig) Includes() []string {
	includes := config.Include
	config.Include = nil
	return includes
}

func run(ctx context.Context, command *cli.Command) error {
	var config serverConfig
	if err := cmd.Load(logger, command.String(cmd.FlagConfig), &config); err != nil {
		return err
	}

	server, err := hamview.NewServer(&config.Server, &config.Database)
	if err != nil {
		return err
	}

	return server.Run()
}
