package logoimp

import (
	"context"

	api "github.com/tech/sendico/pkg/api/http"
	"github.com/tech/sendico/pkg/db/account"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/mservice"
	eapi "github.com/tech/sendico/server/interface/api"
	mutil "github.com/tech/sendico/server/internal/mutil/param"
	"go.uber.org/zap"
)

type LogoAPI struct {
	logger mlogger.Logger
	adb    account.DB
}

func (a *LogoAPI) Name() mservice.Type {
	return mservice.Logo
}

func (a *LogoAPI) Finish(ctx context.Context) error {
	return nil
}

func CreateAPI(a eapi.API) (*LogoAPI, error) {
	p := new(LogoAPI)
	p.logger = a.Logger().Named(p.Name())
	var err error

	if p.adb, err = a.DBFactory().NewAccountDB(); err != nil {
		p.logger.Error("Failed to create account database", zap.Error(err))
		return nil, err
	}
	a.Register().Handler(mservice.Logo, mutil.AddAccountRef("/")+"/{email_type}", api.Get, p.getLogo)

	return p, nil
}
