package mdb

import (
	"context"
	"kurumibot/laniakea"
	"os"
	"time"
)

type ConsoleLogEntry struct {
	Level     string    `bson:"level"`
	Prefix    string    `bson:"prefix"`
	Traceback string    `bson:"traceback"`
	Message   string    `bson:"message"`
	Time      time.Time `bson:"time"`
	TimeStamp int64     `bson:"timeStamp"`
}

func WriteConsoleLog(db *laniakea.DatabaseContext, e *ConsoleLogEntry) error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	col := db.MongoDB.Database(os.Getenv("MONGO_NAME")).Collection("logs")
	_, err := col.InsertOne(ctx, e)
	return err
}

type MessageLogEntry struct {
	MessageID int    `bson:"messageId"`
	SenderID  int    `bson:"senderId"`
	ChatID    int    `bson:"chatId"`
	Text      string `bson:"text"`
	TimeStamp int64  `bson:"timestamp"`
}

func WriteMessageLog(db *laniakea.DatabaseContext, e *MessageLogEntry) error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	collection := db.MongoDB.Database(os.Getenv("MONGO_NAME")).Collection("msg_logs")
	_, err := collection.InsertOne(ctx, e)
	return err
}
