package main

import (
	"fmt"
	"os"

	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"markdown-editor-backend/server"
)

var (
	dataDir string
	port    int
	host    string
	log     = logrus.New()
)

var rootCmd = &cobra.Command{
	Use:   "markdown-editor-backend",
	Short: "WYSIWYG Markdown Editor Backend",
	Long:  `A Go backend server for the WYSIWYG Markdown Editor with CRUD operations for markdown files.`,
	Run: func(cmd *cobra.Command, args []string) {
		log.SetFormatter(&logrus.TextFormatter{
			FullTimestamp: true,
		})
		log.SetLevel(logrus.InfoLevel)

		log.WithFields(logrus.Fields{
			"dataDir": dataDir,
			"host":    host,
			"port":    port,
		}).Info("Starting server")

		srv := server.New(dataDir, host, port, log)
		if err := srv.Start(); err != nil {
			log.WithError(err).Fatal("Server failed to start")
		}
	},
}

func init() {
	rootCmd.Flags().StringVar(&dataDir, "data-dir", "./data", "Storage path for markdown files")
	rootCmd.Flags().IntVar(&port, "port", 8080, "Server port")
	rootCmd.Flags().StringVar(&host, "host", "127.0.0.1", "Bind address")
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
