package handlers

// Regression tests for the generated-document download name
// (t-paliad-354): "<YYYY-MM-DD> <keyword> (<case number>).docx".
// The date segment is environment-dependent (Europe/Berlin "today"),
// so the assertions pin the keyword + bracketed case-number frame and
// the .docx suffix rather than the literal date.

import (
	"strings"
	"testing"
	"time"

	"mgit.msbls.de/m/paliad/internal/models"
	"mgit.msbls.de/m/paliad/internal/services"
)

func strptr(s string) *string { return &s }

func todayBerlin() string {
	day := time.Now()
	if loc, err := time.LoadLocation("Europe/Berlin"); err == nil {
		day = day.In(loc)
	}
	return day.Format("2006-01-02")
}

func TestSubmissionFileName(t *testing.T) {
	t.Parallel()
	rule := &models.DeadlineRule{Name: "Klageerwiderung", NameEN: "Statement of defence"}
	date := todayBerlin()

	cases := []struct {
		name    string
		rule    *models.DeadlineRule
		project *models.Project
		lang    string
		keyword string
		want    string
	}{
		{
			name:    "full data — rule name + case number",
			rule:    rule,
			project: &models.Project{CaseNumber: strptr("UPC_CFI_123_2026")},
			lang:    "de",
			want:    date + " Klageerwiderung (UPC_CFI_123_2026).docx",
		},
		{
			name:    "missing case number falls back to placeholder",
			rule:    rule,
			project: &models.Project{CaseNumber: nil},
			lang:    "de",
			want:    date + " Klageerwiderung (Az. folgt).docx",
		},
		{
			name:    "user override keyword wins over rule name",
			rule:    rule,
			project: &models.Project{CaseNumber: strptr("UPC_CFI_123_2026")},
			lang:    "de",
			keyword: "Replik Hauptantrag",
			want:    date + " Replik Hauptantrag (UPC_CFI_123_2026).docx",
		},
		{
			name:    "EN lang uses NameEN when no override",
			rule:    rule,
			project: &models.Project{CaseNumber: strptr("UPC_CFI_123_2026")},
			lang:    "en",
			want:    date + " Statement of defence (UPC_CFI_123_2026).docx",
		},
		{
			name:    "case number containing slash is sanitised inside brackets",
			rule:    rule,
			project: &models.Project{CaseNumber: strptr("UPC_CFI_123/2026")},
			lang:    "de",
			want:    date + " Klageerwiderung (UPC_CFI_123_2026).docx",
		},
		{
			name:    "blank override falls back to rule name",
			rule:    rule,
			project: &models.Project{CaseNumber: strptr("UPC_CFI_123_2026")},
			lang:    "de",
			keyword: "   ",
			want:    date + " Klageerwiderung (UPC_CFI_123_2026).docx",
		},
		{
			name:    "empty rule name + no override falls back to submission",
			rule:    &models.DeadlineRule{Name: "", NameEN: ""},
			project: &models.Project{CaseNumber: nil},
			lang:    "de",
			want:    date + " submission (Az. folgt).docx",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := submissionFileName(tc.rule, tc.project, tc.lang, tc.keyword)
			if got != tc.want {
				t.Errorf("submissionFileName() = %q, want %q", got, tc.want)
			}
			if !strings.HasSuffix(got, ".docx") {
				t.Errorf("filename %q missing .docx suffix", got)
			}
		})
	}
}

func TestSubmissionFilenameKeyword(t *testing.T) {
	t.Parallel()
	cases := []struct {
		name  string
		draft *services.SubmissionDraft
		want  string
	}{
		{"nil draft", nil, ""},
		{"nil meta", &services.SubmissionDraft{}, ""},
		{
			"key absent",
			&services.SubmissionDraft{ComposerMeta: map[string]any{"other": "x"}},
			"",
		},
		{
			"key set",
			&services.SubmissionDraft{ComposerMeta: map[string]any{"filename_keyword": "Replik"}},
			"Replik",
		},
		{
			"key set with surrounding whitespace is trimmed",
			&services.SubmissionDraft{ComposerMeta: map[string]any{"filename_keyword": "  Replik  "}},
			"Replik",
		},
		{
			"non-string value ignored",
			&services.SubmissionDraft{ComposerMeta: map[string]any{"filename_keyword": 42}},
			"",
		},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if got := submissionFilenameKeyword(tc.draft); got != tc.want {
				t.Errorf("submissionFilenameKeyword() = %q, want %q", got, tc.want)
			}
		})
	}
}
