package fiko_mongo

import (
	"context"
	"encoding/json"
	"fmt"
	"image"

	"log"
	"time"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/session"
	"github.com/gofiber/storage/mongodb"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/gridfs"
	"go.mongodb.org/mongo-driver/mongo/options"
	"golang.org/x/image/webp"
)

var Client *mongo.Client
var Session *session.Store
var MongoStore *mongodb.Storage
var SessionConfig session.Config

func Init(app *fiber.App) {
	// MongoDB bağlantı URL'si
	databaseURL := "mongodb://127.0.0.1:8001/"
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)

	// MongoDB bağlantısını kuruyoruz
	client, err := mongo.Connect(ctx, options.Client().ApplyURI(databaseURL))
	if err != nil {
		log.Fatal(err)
	}

	// Bağlantıyı global Client değişkenine atıyoruz
	Client = client
	setMiddleWare(app)
	setSessionMotor(app)
	setRoutes(app)

}

// GetMongoDBClient fonksiyonu mevcut MongoDB bağlantısını döndürür
func GetMongoDBClient() *mongo.Client {
	return Client
}

func GetSession() *session.Store {
	return Session
}

func setSessionMotor(_ *fiber.App) { // Session middleware
	// MongoDB'yi storage olarak ayarlıyoruz
	MongoStore = mongodb.New(mongodb.Config{
		Host:       "127.0.0.1",
		Port:       8001,
		Database:   "fikoyt",
		Collection: "go_sessions",
		Reset:      false,
	})

	// Session middleware yapılandırması
	SessionConfig = session.Config{
		Storage: MongoStore, CookieSecure: true, CookieHTTPOnly: true, CookieSameSite: "lax", Expiration: 24 * time.Hour, KeyLookup: "cookie:sid",
	}
	Session = session.New(SessionConfig)
	Session.RegisterType(bson.M{})
}

func setRoutes(app *fiber.App) {
	app.Get("/fikomongo", func(c fiber.Ctx) error {
		// ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
		// // MongoDB koleksiyonundan veri almak
		// collection := GetMongoDBClient().Database("fikoyt").Collection("hareketler")

		// // Find sorgusuna limit ve skip parametrelerini ekleyerek verileri alıyoruz
		// findOptions := options.Find()
		// findOptions.SetLimit(2)
		// findOptions.SetSkip(0)

		// cursor, err := collection.Find(ctx, bson.D{}, findOptions)
		// if err != nil {
		// 	log.Fatal(err)
		// }
		// defer cursor.Close(ctx)

		// // Verileri tutacak bir dilim
		// var results []bson.M
		// cursor.All(ctx, &results)

		// Eğer cursor'dan çıkarken hata varsa
		// if err := cursor.Err(); err != nil {
		// 	log.Fatal(err)
		// }
		session := c.Locals("session").(*session.Session)

		SetSessionData(session, "test", bson.M{"Test": "Deneme"})
		// Session verisini JSON olarak döndürüyoruz
		return c.Status(200).JSON(true)
	})

	/**/
	app.Get("/fikoyt.com.FikoYT.sistem.aktifKullanici.hareketler.:id.resim", func(c fiber.Ctx) error {
		objectIDStr := c.Params("id")

		if objectIDStr == "" {
			return c.Status(fiber.StatusBadRequest).SendString("ObjectId parametresi gerekli")
		}

		objectID, err := primitive.ObjectIDFromHex(objectIDStr)
		if err != nil {
			return c.Status(fiber.StatusBadRequest).SendString("Geçersiz ObjectId")
		}

		// GridFS'den dosya okuyun
		fsName := "go_files"
		bucket, err := gridfs.NewBucket(GetMongoDBClient().Database("fikoyt"), &options.BucketOptions{Name: &fsName})
		if err != nil {
			return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("GridFS bucket oluşturulamadı: %v", err))
		}

		stream, err := bucket.OpenDownloadStream(objectID)
		if err != nil {
			return c.Status(fiber.StatusNotFound).SendString(fmt.Sprintf("Dosya indirilemiyor: %v", err))
		}
		defer stream.Close()

		// Resmi decode et
		img, _, err := image.Decode(stream)
		if err != nil {
			return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("Resim decode edilemedi: %v", err))
		}

		// Yanıt içerik türünü ayarla
		c.Set("Content-Type", "image/webp")

		// WebP formatına dönüştürme
		err = webp.Encode(c.Response().BodyWriter(), img, &webp.Options{Quality: 80})
		if err != nil {
			return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("WebP formatına dönüştürme hatası: %v", err))
		}

		return nil
	})
}

func setMiddleWare(app *fiber.App) {
	app.Use(func(c fiber.Ctx) error {
		ses, _ := GetSession().Get(c)
		c.Locals("session", ses)
		return c.Next()
	})
}

// Dinamik veriyi session'a kaydeden yardımcı fonksiyon
func SetSessionData(s *session.Session, key string, value interface{}) error {
	// Veriyi JSON'a dönüştürmeye çalışıyoruz
	data, err := json.Marshal(value)
	if err != nil {
		return fmt.Errorf("failed to marshal value: %v", err)
	}

	// Veriyi session'a kaydediyoruz
	s.Set(key, data)

	// Session'ı kaydediyoruz
	if err := s.Save(); err != nil {
		return fmt.Errorf("failed to save session: %v", err)
	}

	return nil
}

// Session'dan veriyi almak için yardımcı fonksiyon
func GetSessionData(s *session.Session, key string) (interface{}, error) {
	// Session'dan veriyi alıyoruz
	value := s.Get(key)
	if value == nil {
		return nil, fmt.Errorf("no value found for key: %s", key)
	}

	// Veriyi JSON'dan çözümleyip interface{} olarak döndürüyoruz
	data, ok := value.([]byte) // Veriyi []byte olarak aldığınızı varsayıyoruz
	if !ok {
		return nil, fmt.Errorf("expected []byte, got %T", value)
	}

	var result interface{}
	if err := json.Unmarshal(data, &result); err != nil {
		return nil, fmt.Errorf("failed to unmarshal session data: %v", err)
	}

	return result, nil
}
