package services

import (
	"context"
	"strings"
	"testing"

	"wa-assistant/backend/models"
)

func TestConversationStyleScenarios(t *testing.T) {
	for _, tc := range []struct {
		name, tone, message, reply, issue string
		history                           []models.ChatHistory
	}{
		{name: "warm greeting", tone: "ramah", message: "Halo", reply: "Halo kak 😊"},
		{name: "formal greeting", tone: "formal", message: "Halo", reply: "Halo 😊", issue: "emoji_context"},
		{name: "complaint", tone: "santai", message: "Kecewa, pesanan saya rusak", reply: "Maaf atas kendalanya 😊", issue: "emoji_context"},
		{name: "complaint punctuation", tone: "ramah", message: "Pembayaran gagal", reply: "Coba lagi!", issue: "exclamation_context"},
		{name: "transaction", tone: "santai", message: "Berapa total pembayaran?", reply: "Totalnya Rp100.000!", issue: "exclamation_context"},
		{name: "customer preference", tone: "ramah", message: "Halo, jangan pakai emoji", reply: "Halo 😊", issue: "emoji_context"},
		{name: "carry complaint", tone: "ramah", message: "Iya", reply: "Siap 😊", issue: "emoji_context", history: []models.ChatHistory{{Message: "Barangnya rusak"}}},
		{name: "no repeated emoji", tone: "ramah", message: "Terima kasih", reply: "Sama-sama 😊", issue: "emoji_context", history: []models.ChatHistory{{Reply: "Halo 😊"}}},
		{name: "resolved", tone: "ramah", message: "Sudah beres, terima kasih", reply: "Sama-sama 😊", history: []models.ChatHistory{{Message: "Pembayaran gagal"}}},
		{name: "excess enthusiasm", tone: "ramah", message: "Halo", reply: "Halo!!", issue: "exclamation_context"},
		{name: "neutral punctuation", tone: "ramah", message: "Pembayaran gagal", reply: "Pesan kesalahan apa yang muncul?"},
		{name: "link preserved", tone: "formal", message: "Tautannya?", reply: "Buka https://example.com/path!", issue: ""},
	} {
		t.Run(tc.name, func(t *testing.T) {
			p := withConversationStyle(responsePolicyForLength(selectAIResponsePolicy(tc.message, "", "", 1), "short", tc.message), tc.tone, tc.message, tc.history)
			if got := conversationStyleIssue(tc.reply, p.Conversation); got != tc.issue {
				t.Fatalf("issue=%q want=%q", got, tc.issue)
			}
			if !strings.Contains(responsePolicyInstruction(p), "EMOJI DAN TANDA BACA") {
				t.Fatal("style policy missing from provider instruction")
			}
		})
	}
}

func TestConversationStyleCheckedBeforeAgentApprovesAnswer(t *testing.T) {
	agenticTestDB(t)
	p := withConversationStyle(responsePolicyForLength(selectAIResponsePolicy("Halo", "", "", 0), "short", "Halo"), "formal", "Halo", nil)
	s := &agentTurnState{reviewAnswer: approveTestAnswer, userMessage: "Halo", policy: p}
	bad := s.checkAnswer(context.Background(), 1, answerCheckInput{Answer: "Halo 😊", Kind: "social"})
	if bad.Approved || bad.Issue != "emoji_context" || !strings.Contains(bad.Guidance, "TANPA emoji") || s.approved != nil {
		t.Fatalf("bad delivery approved: %+v", bad)
	}
	good := s.checkAnswer(context.Background(), 1, answerCheckInput{Answer: "Selamat pagi.", Kind: "social"})
	if !good.Approved || s.approved == nil {
		t.Fatalf("neutral repair not approved: %+v", good)
	}
	if sourcePriorityBoost("correction") != sourcePriorityBoost("manual") {
		t.Fatal("operator correction ranks below manual knowledge")
	}
}
