package paymentapp

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/domain/payment/gateway"
	"git.gocasts.ir/ebhomengo/niki/domain/payment/repository"
	"git.gocasts.ir/ebhomengo/niki/domain/payment/service"
	"git.gocasts.ir/ebhomengo/niki/paymentapp/delivery/grpc"
	"git.gocasts.ir/ebhomengo/niki/pkg/database"
	sgrpc "git.gocasts.ir/ebhomengo/niki/pkg/grpc"
)

type App struct {
	paymentrepo       service.PaymentRepo
	paymentMethodRepo service.PaymentMethodRepo
	gwFactory         service.GatewayFactory
	paymentService    *service.PaymentService
	paymenthandler    *grpc.Handler
	rpcServer         *sgrpc.RPCServer
	server            grpc.PaymentGrpcServer
}

func Setup(ctx context.Context, cfg Config, conn *database.Database) (*App, error) {
	paymentrepo := repository.NewPaymentRepository(conn.DB)
	paymentMethodRepo := repository.NewPaymentMethodRepository(conn.DB)
	gwFactory := gateway.NewGatewayFactory()
	paymentService := service.NewPaymentService(paymentrepo, paymentMethodRepo, gwFactory)

	paymenthandler := grpc.NewHandler(paymentService)

	rpcServer := sgrpc.New(sgrpc.Config(cfg.Grpc))

	server := grpc.NewPaymentGrpcServer(rpcServer, paymenthandler)

	return &App{
		paymentrepo:       paymentrepo,
		paymentMethodRepo: paymentMethodRepo,
		gwFactory:         gwFactory,
		paymentService:    paymentService,
		paymenthandler:    paymenthandler,
		rpcServer:         rpcServer,
		server:            server,
	}, nil
}

func (a *App) Start() error {
	a.server.Serve()
	return nil
}

func (a *App) Stop(ctx context.Context) error {

	return nil
}
