package data

import (
	"context"
	"errors"
	"os"
	"path/filepath"
	"testing"
	"time"

	"kra/pkg/database/gormkit"
	"kra/pkg/logging"

	"gorm.io/gorm/logger"
)

func TestGORMLoggerUsesApplicationCategories(t *testing.T) {
	root := t.TempDir()
	appLogger, cleanup := logging.NewZapLogger(root, "application.log", logging.Options{Level: "info", Format: "json"})
	databaseLogger := gormkit.NewLogger(appLogger, logger.Info)
	databaseLogger.Trace(context.Background(), time.Now(), func() (string, int64) { return "SELECT 1", 1 }, nil)
	databaseLogger.Trace(context.Background(), time.Now(), func() (string, int64) { return "SELECT missing", 0 }, errors.New("database failure"))
	cleanup()

	date := time.Now().Format("2006-01-02")
	for _, name := range []string{"application.log", filepath.Join("sql", "application.log"), filepath.Join("error", "error.log")} {
		info, err := os.Stat(filepath.Join(root, date, name))
		if err != nil || info.Size() == 0 {
			t.Fatalf("expected non-empty GORM log %s: info=%v err=%v", name, info, err)
		}
	}
}
