package purchaseapp

import (
	"context"
	"fmt"
	purchaseMysql "git.gocasts.ir/ebhomengo/niki/domain/order/repository/mysql"
	purchaseService "git.gocasts.ir/ebhomengo/niki/domain/order/service"
	purchaseHTTP "git.gocasts.ir/ebhomengo/niki/purchaseapp/delivery/http"
	purchaseHandler "git.gocasts.ir/ebhomengo/niki/purchaseapp/delivery/http/order"
	"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)

type Application struct {
	Config          Config
	HTTPServer      *purchaseHTTP.Server
	purchaseService purchaseService.Service
	PurchaseHandler *purchaseHandler.Handler
	PurchaseRepo    purchaseService.Repo
	DB              *mysql.DB
}

func SetUp(ctx context.Context, config Config, DB mysql.DB) *Application {

	//cfg := mysql.Config{
	//	Username: "niki",
	//	Password: "nikiappt0lk2o20",
	//	Port:     3306,
	//	Host:     "localhost",
	//	DBName:   "niki_db",
	//}
	db := mysql.New(config.Mysql)
	defer func() {
		if err := db.CloseStatements(); err != nil {
			fmt.Printf("Error closing statements: %v\n", err)
		}
	}()

	orderRepo := purchaseMysql.New(db)

	orderSvc := Service(orderRepo)
	server := HTTPServer(orderSvc)
	handler := purchaseHandler.New(orderSvc)

	return &Application{
		Config:          Config{},
		HTTPServer:      server,
		purchaseService: orderSvc,
		PurchaseHandler: handler,
		PurchaseRepo:    orderRepo,
		DB:              &DB,
	}

}

func HTTPServer(orderSvc purchaseService.Service) *purchaseHTTP.Server {
	return purchaseHTTP.New(orderSvc)
}

func Service(orderRepo *purchaseMysql.DB) purchaseService.Service {
	return purchaseService.New(orderRepo)
}
