package server

import (
	"fmt"
	"log"
	"music-server/internal/db"
	"net/http"
	"os"
	"strconv"
	"time"
)

type Server struct {
	port int
}

var (
	host      = os.Getenv("DB_HOST")
	dbPort    = os.Getenv("DB_PORT")
	dbName    = os.Getenv("DB_NAME")
	username  = os.Getenv("DB_USERNAME")
	password  = os.Getenv("DB_PASSWORD")
	musicPath = os.Getenv("MUSIC_PATH")
)

func NewServer() *http.Server {

	port, _ := strconv.Atoi(os.Getenv("PORT"))
	NewServer := &Server{
		port: port,
	}

	//conf.SetupDb()
	if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" {
		log.Fatal("Invalid settings")
	}

	fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
		host, dbPort, username, password, dbName)

	log.Printf("Path: %s\n", musicPath)

	db.Migrate_db(host, dbPort, username, password, dbName)

	db.InitDB(host, dbPort, username, password, dbName)

	// Declare Server config
	server := &http.Server{
		Addr:         fmt.Sprintf(":%d", NewServer.port),
		Handler:      NewServer.RegisterRoutes(),
		IdleTimeout:  time.Minute,
		ReadTimeout:  10 * time.Second,
		WriteTimeout: 30 * time.Second,
	}

	return server
}
