package api

import (
	"log/slog"
	"net/http"

	"code.gitea.io/sdk/gitea"
	"git.schreifuchs.ch/lou-taylor/accounting/internal/api/httpinvoce"
	"git.schreifuchs.ch/lou-taylor/accounting/internal/config"
	"git.schreifuchs.ch/lou-taylor/accounting/internal/email"
	"git.schreifuchs.ch/lou-taylor/accounting/internal/pdf"
	"git.schreifuchs.ch/lou-taylor/accounting/pkg/invoice"
)

func RegisterRoutes(log *slog.Logger, mux *http.ServeMux, cfg *config.Config) error {
	gotenberg, err := pdf.New(cfg.PDF.Hostname)
	if err != nil {
		panic(err)
	}
	giteaC, err := gitea.NewClient(
		cfg.Gitea.URL,
		gitea.SetToken(cfg.Gitea.Token),
	)
	if err != nil {
		panic(err)
	}

	invoicer := invoice.New(log, giteaC, gotenberg)
	mailer, err := email.New(cfg.Email)
	if err != nil {
		return err
	}

	httpinvoce.New(log, invoicer, mailer).RegisterRoutes(mux)
	return nil
}
