package app

import (
	"testing"
	"time"

	"github.com/labstack/echo/v5"
	"github.com/stretchr/testify/assert"
)

// TestApp_New tests App constructor
func TestApp_New(t *testing.T) {
	e := echo.New()

	app := New(e)

	assert.NotNil(t, app, "App should not be nil")
	assert.Equal(t, e, app.echo, "Echo instance should be stored")
	assert.Equal(t, 30*time.Second, app.shutdownTimeout, "Default shutdown timeout should be 30 seconds")
	assert.NotNil(t, app.shutdownDone, "Shutdown done channel should be initialized")
}

// TestApp_SetShutdownTimeout tests configurable shutdown timeout
func TestApp_SetShutdownTimeout(t *testing.T) {
	e := echo.New()
	app := New(e)

	customTimeout := 15 * time.Second
	app.SetShutdownTimeout(customTimeout)

	assert.Equal(t, customTimeout, app.shutdownTimeout, "Shutdown timeout should be updated")
}

// TestApp_ShutdownDone tests shutdown done channel
func TestApp_ShutdownDone(t *testing.T) {
	e := echo.New()
	app := New(e)

	channel := app.ShutdownDone()
	assert.NotNil(t, channel, "ShutdownDone should return a channel")
	// Verify it's the same channel by checking if it's readable
	select {
	case <-channel:
		// Channel should not be closed yet
		t.Error("ShutdownDone channel should not be closed immediately")
	default:
		// Expected - channel is open but not ready
	}
}

// BenchmarkApp_Shutdown benchmarks the shutdown process
func BenchmarkApp_Shutdown(b *testing.B) {
	e := echo.New()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		app := New(e)
		err := app.Shutdown()
		if err != nil {
			return
		}
	}
}
