package app

import (
	"testing"

	"github.com/stretchr/testify/require"
)

func TestLoadConfigKeepsNotificationDestinationSeparateFromIngressAllowlist(t *testing.T) {
	t.Setenv("AUTO_ONCALL_OPTIMIZER_LARK_APP_ID", "cli_test")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_LARK_APP_SECRET", "secret")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_LARK_BOT_OPEN_ID", "ou_bot")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_ADMIN_TOKEN", "admin")
	t.Setenv("CODEX_HOME", "/tmp/codex-home")
	t.Setenv("GH_TOKEN", "github")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_ALLOWED_CHAT_IDS", "oc_command")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_ALLOWED_SENDER_OPEN_IDS", "ou_owner")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_PR_SENDER_OPEN_IDS", "ou_owner")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_AUTO_TRIGGER_ENABLED", "true")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_NOTIFY_CHAT_ID", "oc_notifications")

	config, err := LoadConfig()
	require.NoError(t, err)
	require.Equal(t, "oc_notifications", config.NotifyChatID)
	require.Contains(t, config.AllowedChatIDs, "oc_command")
	require.NotContains(t, config.AllowedChatIDs, config.NotifyChatID)
}

func TestLoadConfigEnablesWeekdayFeedbackReminderInAllowedChat(t *testing.T) {
	t.Setenv("AUTO_ONCALL_OPTIMIZER_LARK_APP_ID", "cli_test")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_LARK_APP_SECRET", "secret")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_LARK_BOT_OPEN_ID", "ou_bot")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_ADMIN_TOKEN", "admin")
	t.Setenv("CODEX_HOME", "/tmp/codex-home")
	t.Setenv("GH_TOKEN", "github")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_ALLOWED_CHAT_IDS", "oc_feedback")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_ALLOWED_SENDER_OPEN_IDS", "ou_owner")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_PR_SENDER_OPEN_IDS", "ou_owner")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_FEEDBACK_REMINDER_ENABLED", "true")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_FEEDBACK_REMINDER_CHAT_ID", "oc_feedback")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_FEEDBACK_REMINDER_TIME_ZONE", "Asia/Shanghai")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_FEEDBACK_REMINDER_HOUR", "17")
	t.Setenv("AUTO_ONCALL_OPTIMIZER_FEEDBACK_REMINDER_LOOKBACK_DAYS", "14")
	t.Setenv("JIRA_USERNAME", "readonly@example.com")
	t.Setenv("JIRA_API_TOKEN", "jira-token")
	t.Setenv("AUTO_ONCALL_DATABASE_URL", "mysql://readonly:secret@db.example:4000/auto_oncall_staging")

	config, err := LoadConfig()
	require.NoError(t, err)
	require.True(t, config.FeedbackReminderEnabled)
	require.Equal(t, "oc_feedback", config.FeedbackReminderChatID)
	require.Equal(t, "Asia/Shanghai", config.FeedbackReminderLocation.String())
	require.Equal(t, 17, config.FeedbackReminderHour)
	require.Equal(t, 14, config.FeedbackReminderLookbackDays)
}
