package logger

import (
	"os"
	"testing"
	"time"
)

func TestDynamicLogging(t *testing.T) {
	// Pastikan direktori data ada
	os.RemoveAll("pkg/logger/data")

	t.Run("TestSaveLogText", testSaveLogText)
	t.Run("TestSaveLogJSON", testSaveLogJSON)
	t.Run("TestSaveLogToDatabase", testSaveLogToDatabase)
}

func testSaveLogText(t *testing.T) {
	logger := New("test-service", INFO, false)

	entry := LogEntry{
		Timestamp: time.Now().Format(time.RFC3339),
		Level:     "INFO",
		Service:   "test-service",
		Message:   "Test log message",
		File:      "test.go",
		Line:      10,
		Fields: map[string]interface{}{
			"test_field": "test_value",
			"number":     42,
		},
	}

	err := logger.SaveLogText(entry)
	if err != nil {
		t.Errorf("SaveLogText failed: %v", err)
	}

	// Verifikasi file dibuat
	if _, err := os.Stat("pkg/logger/data/logs.txt"); os.IsNotExist(err) {
		t.Error("Text log file was not created")
	}
}

func testSaveLogJSON(t *testing.T) {
	logger := New("test-service", INFO, false)

	entry := LogEntry{
		Timestamp: time.Now().Format(time.RFC3339),
		Level:     "INFO",
		Service:   "test-service",
		Message:   "Test JSON log message",
		File:      "test.go",
		Line:      20,
		Fields: map[string]interface{}{
			"json_field": "json_value",
			"count":      100,
		},
	}

	err := logger.SaveLogJSON(entry)
	if err != nil {
		t.Errorf("SaveLogJSON failed: %v", err)
	}

	// Verifikasi file dibuat
	if _, err := os.Stat("pkg/logger/data/logs.json"); os.IsNotExist(err) {
		t.Error("JSON log file was not created")
	}
}

func testSaveLogToDatabase(t *testing.T) {
	logger := New("test-service", INFO, false)

	entry := LogEntry{
		Timestamp: time.Now().Format(time.RFC3339),
		Level:     "INFO",
		Service:   "test-service",
		Message:   "Test database log message",
		File:      "test.go",
		Line:      30,
	}

	err := logger.SaveLogToDatabase(entry)
	if err != nil {
		t.Errorf("SaveLogToDatabase failed: %v", err)
	}

	// Verifikasi file dibuat (placeholder untuk database)
	if _, err := os.Stat("pkg/logger/data/database_logs.txt"); os.IsNotExist(err) {
		t.Error("Database log file was not created")
	}
}
