package serverimp

import (
	"context"
	"os"
	"time"

	"github.com/tech/sendico/billing/documents/internal/docstore"
	"github.com/tech/sendico/billing/documents/internal/service/documents"
	"github.com/tech/sendico/billing/documents/storage"
	mongostorage "github.com/tech/sendico/billing/documents/storage/mongo"
	"github.com/tech/sendico/pkg/api/routers"
	"github.com/tech/sendico/pkg/db"
	msg "github.com/tech/sendico/pkg/messaging"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/server/grpcapp"
	"go.uber.org/zap"
	"gopkg.in/yaml.v3"
)

type Imp struct {
	logger  mlogger.Logger
	file    string
	debug   bool
	config  *config
	app     *grpcapp.App[storage.Repository]
	service *documents.Service
}

type config struct {
	*grpcapp.Config `yaml:",inline"`

	Documents documents.Config `yaml:"documents"`
}

// Create initialises the billing documents server implementation.
func Create(logger mlogger.Logger, file string, debug bool) (*Imp, error) {
	return &Imp{
		logger: logger.Named("server"),
		file:   file,
		debug:  debug,
	}, nil
}

func (i *Imp) Shutdown() {
	if i.app == nil {
		if i.service != nil {
			i.service.Shutdown()
		}

		return
	}

	timeout := 15 * time.Second
	if i.config != nil && i.config.Runtime != nil {
		timeout = i.config.Runtime.ShutdownTimeout()
	}

	if i.service != nil {
		i.service.Shutdown()
	}

	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	i.app.Shutdown(ctx)
	cancel()
}

func (i *Imp) Start() error {
	cfg, err := i.loadConfig()
	if err != nil {
		return err
	}

	i.config = cfg

	repoFactory := func(logger mlogger.Logger, conn *db.MongoConnection) (storage.Repository, error) {
		return mongostorage.New(logger, conn)
	}

	docStore, err := docstore.New(i.logger, cfg.Documents.Storage)
	if err != nil {
		i.logger.Error("Failed to initialise document storage", zap.Error(err))

		return err
	}

	serviceFactory := func(logger mlogger.Logger, repo storage.Repository, producer msg.Producer) (grpcapp.Service, error) { //nolint:lll // factory signature dictated by grpcapp
		invokeURI := ""
		if cfg.GRPC != nil {
			invokeURI = cfg.GRPC.DiscoveryInvokeURI()
		}

		svc := documents.NewService(logger, repo, producer,
			documents.WithDiscoveryInvokeURI(invokeURI),
			documents.WithConfig(cfg.Documents),
			documents.WithDocumentStore(docStore),
		)
		i.service = svc

		return svc, nil
	}

	app, err := grpcapp.NewApp(i.logger, "billing_documents", cfg.Config, i.debug, repoFactory, serviceFactory)
	if err != nil {
		return err
	}

	i.app = app

	return i.app.Start()
}

func (i *Imp) loadConfig() (*config, error) {
	data, err := os.ReadFile(i.file)
	if err != nil {
		i.logger.Error("Could not read configuration file", zap.String("config_file", i.file), zap.Error(err))

		return nil, err
	}

	cfg := &config{Config: &grpcapp.Config{}}
	if err := yaml.Unmarshal(data, cfg); err != nil {
		i.logger.Error("Failed to parse configuration", zap.Error(err))

		return nil, err
	}

	if cfg.Runtime == nil {
		cfg.Runtime = &grpcapp.RuntimeConfig{ShutdownTimeoutSeconds: 15}
	}

	if cfg.GRPC == nil {
		cfg.GRPC = &routers.GRPCConfig{
			Network:          "tcp",
			Address:          ":50061",
			EnableReflection: true,
			EnableHealth:     true,
		}
	}

	if cfg.Metrics == nil {
		cfg.Metrics = &grpcapp.MetricsConfig{Address: ":9409"}
	}

	if cfg.Documents.Storage.Driver == "" {
		cfg.Documents.Storage.Driver = docstore.DriverLocal
		if cfg.Documents.Storage.Local == nil {
			cfg.Documents.Storage.Local = &docstore.LocalConfig{RootPath: "tmp/documents"}
		}
	}

	return cfg, nil
}
