package database

import (
	"log/slog"
	"os"
	"path/filepath"
	"testing"
	"time"

	"git.eeqj.de/sneak/ipapi/internal/config"
	"git.eeqj.de/sneak/ipapi/internal/state"
)

func TestNeedsUpdate(t *testing.T) {
	tmpFile, err := os.CreateTemp("", "test-db")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(tmpFile.Name())
	tmpFile.Close()

	tests := []struct {
		name         string
		filePath     string
		lastDownload time.Time
		expected     bool
	}{
		{
			name:         "file doesn't exist",
			filePath:     "/nonexistent/file",
			lastDownload: time.Now(),
			expected:     true,
		},
		{
			name:         "recent download",
			filePath:     tmpFile.Name(),
			lastDownload: time.Now().Add(-time.Hour),
			expected:     false,
		},
		{
			name:         "old download",
			filePath:     tmpFile.Name(),
			lastDownload: time.Now().Add(-8 * 24 * time.Hour),
			expected:     true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := needsUpdate(tt.filePath, tt.lastDownload)
			if result != tt.expected {
				t.Errorf("expected %v, got %v", tt.expected, result)
			}
		})
	}
}

func TestNew(t *testing.T) {
	tmpDir, err := os.MkdirTemp("", "ipapi-db-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	cfg := &config.Config{
		StateDir: tmpDir,
	}
	logger := slog.Default()
	stateManager, err := state.New(cfg, logger)
	if err != nil {
		t.Fatal(err)
	}

	manager, err := New(cfg, logger, stateManager)
	if err != nil {
		t.Fatalf("failed to create database manager: %v", err)
	}

	if manager == nil {
		t.Fatal("expected manager, got nil")
	}

	expectedDataDir := filepath.Join(tmpDir, "databases")
	if manager.dataDir != expectedDataDir {
		t.Errorf("expected data dir %s, got %s", expectedDataDir, manager.dataDir)
	}
}
