package main

import (
	"flag"
	"fmt"
	purchaseMysql "git.gocasts.ir/ebhomengo/niki/domain/purchase/repository/mysql"
	"git.gocasts.ir/ebhomengo/niki/purchaseapp/delivery/http"
	"git.gocasts.ir/ebhomengo/niki/purchaseapp/service/order"
	"git.gocasts.ir/ebhomengo/niki/repository/migrator"
	"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)

func MariaDB() *mysql.DB {
	cfg := mysql.Config{
		Username: "niki",
		Password: "nikiappt0lk2o20",
		Port:     3306,
		Host:     "localhost",
		DBName:   "niki_db",
	}
	migrate := flag.Bool("migrate", false, "perform database migration")
	flag.Parse()
	if *migrate {
		migrator.New(migrator.Config{
			MysqlConfig:     cfg,
			MigrationPath:   "./purchaseapp/repository/mysql/migration",
			MigrationDBName: "gorp_migrations",
		}).Up()
	}

	return mysql.New(cfg)
}

func main() {
	cfg := mysql.Config{
		Username: "niki",
		Password: "nikiappt0lk2o20",
		Port:     3306,
		Host:     "localhost",
		DBName:   "niki_db",
	}
	db := mysql.New(cfg)
	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)
	server.Serve()

}

func HTTPServer(orderSvc order.Service) *http.Server {
	return http.New(orderSvc)
}

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