package grib

import (
	"context"
	"testing"
	"time"
)

func TestServiceCreation(t *testing.T) {
	cfg := ServiceConfig{
		Dir:      "/tmp/grib_test",
		TTL:      24 * time.Hour,
		CacheTTL: 1 * time.Hour,
		Redis:    &MockRedis{},
		Parallel: 2,
	}

	service, err := New(cfg)
	if err != nil {
		t.Fatalf("Failed to create service: %v", err)
	}
	defer service.Close()

	if service == nil {
		t.Fatal("Service is nil")
	}
}

func TestNearestRun(t *testing.T) {
	now := time.Date(2024, 1, 15, 14, 30, 0, 0, time.UTC)
	expected := time.Date(2024, 1, 15, 12, 0, 0, 0, time.UTC)

	result := nearestRun(now)
	if !result.Equal(expected) {
		t.Errorf("Expected %v, got %v", expected, result)
	}
}

func TestPressureFromAlt(t *testing.T) {
	alt := 10000.0 // 10km
	pressure := pressureFromAlt(alt)

	// At 10km, pressure should be around 264 hPa
	if pressure < 200 || pressure > 300 {
		t.Errorf("Unexpected pressure at 10km: %f hPa", pressure)
	}
}

// MockRedis for testing
type MockRedis struct{}

func (m *MockRedis) Lock(ctx context.Context, key string, ttl time.Duration) (func(context.Context), error) {
	return func(ctx context.Context) {}, nil
}

func (m *MockRedis) Set(key string, value []byte, ttl time.Duration) error {
	return nil
}

func (m *MockRedis) Get(key string) ([]byte, error) {
	return nil, nil
}
