package main

import (
	"log"
	"os"
	uyar_app_main "server/apps/uyar_dokum/uyar"

	FikoYTCtx "server/sistem/fikoyt_ctx"
	fiko_socket_io "server/sistem/fikoyt_socket_io"
	"server/sistem/kok"

	"github.com/gofiber/fiber/v3"
	recoverer "github.com/gofiber/fiber/v3/middleware/recover"
	"github.com/sinemah/jsonenv"
)

func main() {
	err := jsonenv.Load("fikoyt.env.json")
	if err != nil {
		log.Fatal(err)
	}

	app := fiber.New(fiber.Config{AppName: os.Getenv("uyg_adi") /* ErrorHandler: GlobalErrorHandler,*/, StreamRequestBody: true, Immutable: true, BodyLimit: 2000 * 1024 * 1024})
	app.Use(recoverer.New())
	FikoYTCtx.Ayarla(app)
	/**/
	fiko_socket_io.Ayarla(app)
	uyar_app_main.Ayarla(app)
	//
	kok.KokRouting(app)

	runSslWithPrefork(app)
}

func runSslWithPrefork(app *fiber.App) {
	// SSL sertifikası yükleniyor
	// cert, err := tls.LoadX509KeyPair("cert/server.crt", "cert/server.key")
	// if err != nil {
	// 	log.Fatalf("SSL Sertifikası yüklenirken hata oluştu: %v", err)
	// }

	// TLS yapılandırması
	// tlsConfig := &tls.Config{
	// 	Certificates: []tls.Certificate{cert},
	// }

	// ListenConfig kullanılarak sunucu başlatılıyor
	app.Listen(":"+os.Getenv("port_as_string"), fiber.ListenConfig{
		CertFile:    "cert/server.crt",
		CertKeyFile: "cert/server.key",

		// TLSConfigFunc:         tlsConfig, // SSL yapılandırması
		EnablePrefork:     true, // Prefork etkinleştiriliyor
		EnablePrintRoutes: true, // Rotalar yazdırılsın
	})
}

// func GlobalErrorHandler(c fiber.Ctx, err error) error {
// 	if err == nil {
// 		return c.Status(fiber.StatusInternalServerError).SendString("An unknown error occurred")
// 	}

// 	// Fiber özel hatalarını yönet
// 	if e, ok := err.(*fiber.Error); ok {
// 		return c.Status(e.Code).SendString(e.Message)
// 	}

// 	// Genel hatalar için
// 	log.Printf("Unhandled error: %v", err)
// 	return c.Status(fiber.StatusInternalServerError).SendString("Internal server error")
// }
