package serverimp

import (
	"context"
	"os"
	"time"

	"github.com/tech/sendico/gateway/tgsettle/internal/service/gateway"
	"github.com/tech/sendico/gateway/tgsettle/storage"
	gatewaymongo "github.com/tech/sendico/gateway/tgsettle/storage/mongo"
	"github.com/tech/sendico/pkg/api/routers"
	"github.com/tech/sendico/pkg/db"
	"github.com/tech/sendico/pkg/merrors"
	msg "github.com/tech/sendico/pkg/messaging"
	mb "github.com/tech/sendico/pkg/messaging/broker"
	"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 *gateway.Service
}

type config struct {
	*grpcapp.Config `yaml:",inline"`
	Gateway         gatewayConfig `yaml:"gateway"`
}

type gatewayConfig struct {
	Rail            string   `yaml:"rail"`
	TargetChatIDEnv string   `yaml:"target_chat_id_env"`
	TimeoutSeconds  int32    `yaml:"timeout_seconds"`
	AcceptedUserIDs []string `yaml:"accepted_user_ids"`
	SuccessReaction string   `yaml:"success_reaction"`
}

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 {
		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)
	defer cancel()
	i.app.Shutdown(ctx)
}

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

	var broker mb.Broker
	if cfg.Messaging != nil && cfg.Messaging.Driver != "" {
		broker, err = msg.CreateMessagingBroker(i.logger, cfg.Messaging)
		if err != nil {
			i.logger.Warn("Failed to create messaging broker", zap.Error(err))
		}
	}

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

	serviceFactory := func(logger mlogger.Logger, repo storage.Repository, producer msg.Producer) (grpcapp.Service, error) {
		invokeURI := ""
		if cfg.GRPC != nil {
			invokeURI = cfg.GRPC.DiscoveryInvokeURI()
		}
		gwCfg := gateway.Config{
			Rail:            cfg.Gateway.Rail,
			TargetChatIDEnv: cfg.Gateway.TargetChatIDEnv,
			TimeoutSeconds:  cfg.Gateway.TimeoutSeconds,
			AcceptedUserIDs: cfg.Gateway.AcceptedUserIDs,
			SuccessReaction: cfg.Gateway.SuccessReaction,
			InvokeURI:       invokeURI,
		}
		svc := gateway.NewService(logger, repo, producer, broker, gwCfg)
		i.service = svc
		return svc, nil
	}

	app, err := grpcapp.NewApp(i.logger, "tgsettle_gateway", 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:          ":50080",
			EnableReflection: true,
			EnableHealth:     true,
		}
	}
	if cfg.Metrics == nil {
		cfg.Metrics = &grpcapp.MetricsConfig{Address: ":9406"}
	}
	if cfg.Gateway.Rail == "" {
		return nil, merrors.InvalidArgument("gateway rail is required", "gateway.rail")
	}
	return cfg, nil
}
