package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	_ "github.com/lib/pq"

	"go.sour.is/pkg/lg"
	"go.sour.is/pkg/mercury/app"
	"go.sour.is/pkg/mercury/http"
	"go.sour.is/pkg/mercury/mqtt"
	"go.sour.is/pkg/mercury/pg"
	"go.sour.is/pkg/service"
	"go.sour.is/pkg/xdg"

	"go.sour.is/pkg/mercury"
)
var baseConfig = `
# mercury.source [handler] [name]
#@mercury.source.http-notify.default

#match    :0 *
#endpoint :http://example.com/webhook
@mercury.source.http-notify.default

#@mercury.source.mqtt-notify.default
#match    :0 *
#endpoint :mqtt://example.com/topic
@mercury.source.http-notify.default

@mercury.source.mercury-default.default
match :1 mercury

@mercury.source.mercury-environ.default
match :1 mercury.host,mercury.environ,mercury.priority,mercury.source.*
`

var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
	ctx,  span := lg.Span(ctx)
	defer span.End()

	conf, err := readConfig(ctx)
	if err != nil {
		return err
	}

	app.Register(conf)
	http.Register()
	mqtt.Register()
	pg.Register()

	err = mercury.Registry.Configure(conf)
	if err != nil {
		return err
	}

	span.AddEvent("Enable Mercury")
	svc.Add(mercury.NewHTTP())

	return nil
})

func readConfig(ctx context.Context) (mercury.SpaceMap, error) {
	_,  span := lg.Span(ctx)
	defer span.End()

	conf, err := mercury.ParseText(strings.NewReader(baseConfig))
	if err != nil {
		return nil, err
	}

	wd, err := os.Getwd()
	if err != err {
		return nil, err
	}

	for _, fn := range xdg.Find(wd+":"+xdg.EnvConfigDirs, "config.mercury") {
		span.AddEvent(fmt.Sprint("config:", fn))

		fd, err := os.Open(fn)
		if err != nil {
			return nil, err
		}
		defer fd.Close()

		c, err := mercury.ParseText(fd)
		if err != nil {
			return nil, err
		}
		conf.MergeMap(c)
	}

	return conf, nil
}