package main

import (
	"strings"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"git.d-ma.be/mathias/tapir/internal/adapters/store"
)

var testSince = time.Date(2026, 6, 11, 0, 0, 0, 0, time.UTC)

func TestFormatReportColumnsAndGatePass(t *testing.T) {
	rows := []store.UserActiveWeeks{
		{UserID: "user-a", DisplayName: "Ada", ActiveWeeks: 3},
		{UserID: "user-b", DisplayName: "", ActiveWeeks: 1},
	}

	var b strings.Builder
	require.NoError(t, formatReport(&b, rows, testSince))
	out := b.String()

	require.Contains(t, out, "since 2026-06-11", "report states the gate baseline date")
	require.Contains(t, out, "USER")
	require.Contains(t, out, "ACTIVE_WEEKS")
	require.Contains(t, out, "Ada")
	require.Contains(t, out, "PASSED", "a user at >= 2 weeks passes the gate")

	// The >=2 user is marked PASS; the 1-week user is not.
	require.Contains(t, lineContaining(t, out, "user-a"), "PASS")
	require.NotContains(t, lineContaining(t, out, "user-b"), "PASS")
	require.Contains(t, lineContaining(t, out, "user-b"), "-", "no display name falls back to dash")
}

func TestFormatReportGateNotMet(t *testing.T) {
	rows := []store.UserActiveWeeks{{UserID: "user-a", ActiveWeeks: 1}}

	var b strings.Builder
	require.NoError(t, formatReport(&b, rows, testSince))
	require.Contains(t, b.String(), "NOT YET MET", "no user at >= 2 weeks fails the gate")
}

func TestFormatReportEmpty(t *testing.T) {
	var b strings.Builder
	require.NoError(t, formatReport(&b, nil, testSince))
	require.Contains(t, b.String(), "no users yet")
}
