package cron

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/ez-api/ez-api/internal/model"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func TestLogCleanerRetentionDeletesOld(t *testing.T) {
	dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
	db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	now := time.Now().UTC()
	old := model.LogRecord{ModelName: "m1", StatusCode: 200}
	old.CreatedAt = now.Add(-48 * time.Hour)
	fresh := model.LogRecord{ModelName: "m1", StatusCode: 200}
	fresh.CreatedAt = now.Add(-2 * time.Hour)

	if err := db.Create(&old).Error; err != nil {
		t.Fatalf("create old: %v", err)
	}
	if err := db.Create(&fresh).Error; err != nil {
		t.Fatalf("create fresh: %v", err)
	}

	cleaner := NewLogCleaner(db, nil, 1, 0, nil)
	if err := cleaner.cleanOnce(context.Background()); err != nil {
		t.Fatalf("clean once: %v", err)
	}

	var remaining int64
	if err := db.Model(&model.LogRecord{}).Count(&remaining).Error; err != nil {
		t.Fatalf("count logs: %v", err)
	}
	if remaining != 1 {
		t.Fatalf("expected 1 log remaining, got %d", remaining)
	}
}

func TestLogCleanerMaxRecordsKeepsLatest(t *testing.T) {
	dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
	db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
	if err != nil {
		t.Fatalf("open sqlite: %v", err)
	}
	if err := db.AutoMigrate(&model.LogRecord{}); err != nil {
		t.Fatalf("migrate: %v", err)
	}

	for i := 0; i < 5; i++ {
		if err := db.Create(&model.LogRecord{ModelName: "m1", StatusCode: 200}).Error; err != nil {
			t.Fatalf("create log %d: %v", i, err)
		}
	}

	cleaner := NewLogCleaner(db, nil, 0, 3, nil)
	if err := cleaner.cleanOnce(context.Background()); err != nil {
		t.Fatalf("clean once: %v", err)
	}

	var remaining int64
	if err := db.Model(&model.LogRecord{}).Count(&remaining).Error; err != nil {
		t.Fatalf("count logs: %v", err)
	}
	if remaining != 3 {
		t.Fatalf("expected 3 logs remaining, got %d", remaining)
	}

	var min model.LogRecord
	if err := db.Order("id asc").First(&min).Error; err != nil {
		t.Fatalf("fetch min log: %v", err)
	}
	if min.ID < 3 {
		t.Fatalf("expected min id >= 3, got %d", min.ID)
	}
}

func TestLogCleanerRunOnceNilSafe(t *testing.T) {
	// Test nil cleaner
	var nilCleaner *LogCleaner
	nilCleaner.RunOnce(context.Background())

	// Test cleaner with nil db
	cleaner := &LogCleaner{}
	cleaner.RunOnce(context.Background())

	// Should not panic
}
