package http

import (
	"flag"
	"fmt"
	"log"
	"net/http"

	_ "embed"

	"gogs.inhome.blapointe.com/ana-ledger/src/ledger"
)

func Main() {
	likeName := flag.String("like-name", ".", "regexp to match")
	likeBefore := flag.String("like-before", "9", "date str to compare")
	likeAfter := flag.String("like-after", "0", "date str to compare")
	groupName := flag.String("group-name", ".*", "grouping to apply to names")
	groupDate := flag.String("group-date", ".*", "grouping to apply to dates")
	bpiPath := flag.String("bpi", "/dev/null", "bpi file")
	httpOutput := flag.String("http", ":8080", "http output listen address, like :8080")
	flag.Parse()

	if flag.NArg() < 1 {
		panic(fmt.Errorf("positional arguments for files required"))
	}

	f, err := ledger.NewFiles(flag.Args()[0], flag.Args()[1:]...)
	if err != nil {
		panic(err)
	}

	r := NewRouter(
		f,
		*likeName,
		*likeBefore,
		*likeAfter,
		*groupName,
		*groupDate,
		*bpiPath,
	)
	log.Println("listening on", *httpOutput)
	if err := http.ListenAndServe(*httpOutput, r); err != nil {
		panic(err)
	}
}
