package zddc

import "testing"

func TestParseTransmittalFolder(t *testing.T) {
	type expect struct {
		date, tracking, status, title string
		ok                            bool
	}
	cases := map[string]expect{
		"2026-06-15_proj-EM-TRN-0042 (DFT) - Foundation Plans": {
			date: "2026-06-15", tracking: "proj-EM-TRN-0042", status: "DFT", title: "Foundation Plans", ok: true,
		},
		"2026-01-15_123456-EM-SUB-0001 (IFR) - Submittal Title.with.dots": {
			date: "2026-01-15", tracking: "123456-EM-SUB-0001", status: "IFR", title: "Submittal Title.with.dots", ok: true,
		},
		"2026-01-15_proj-EM-SUB-0001 (RSA) - Response Notes": {
			date: "2026-01-15", tracking: "proj-EM-SUB-0001", status: "RSA", title: "Response Notes", ok: true,
		},
		// Whitespace variations around "(" and "-".
		"2026-01-15_proj-EM-TRN-1(IFC)-Title": {
			date: "2026-01-15", tracking: "proj-EM-TRN-1", status: "IFC", title: "Title", ok: true,
		},
		// MDL is a valid transmittal name even though it doesn't match TRN/SUB.
		"2026-01-15_proj-EM-MDL-0001 (IFR) - Master Deliverables List": {
			date: "2026-01-15", tracking: "proj-EM-MDL-0001", status: "IFR", title: "Master Deliverables List", ok: true,
		},
		// Negatives.
		"":                     {ok: false},
		"scratch":              {ok: false},
		"2026-06-15 missing-underscore": {ok: false},
		"NotADate_proj-EM-TRN-0042 (DFT) - Title": {ok: false},
		"2026-06-15_no-status - Title":            {ok: false},
		"2026-06-15_no-dash (IFC) Title":          {ok: false},
		// Tracking with whitespace must not parse — tracking has no spaces.
		"2026-06-15_proj EM TRN (IFC) - Title": {ok: false},
	}
	for in, want := range cases {
		date, tracking, status, title, ok := ParseTransmittalFolder(in)
		if ok != want.ok {
			t.Errorf("ParseTransmittalFolder(%q) ok=%v, want %v", in, ok, want.ok)
			continue
		}
		if !ok {
			continue
		}
		if date != want.date || tracking != want.tracking || status != want.status || title != want.title {
			t.Errorf("ParseTransmittalFolder(%q) = (%q,%q,%q,%q), want (%q,%q,%q,%q)",
				in, date, tracking, status, title, want.date, want.tracking, want.status, want.title)
		}
	}
}

func TestParseTransmittalFolderTrailingSlash(t *testing.T) {
	date, tracking, _, _, ok := ParseTransmittalFolder("2026-06-15_proj-TRN-1 (DFT) - x/")
	if !ok || date != "2026-06-15" || tracking != "proj-TRN-1" {
		t.Errorf("trailing slash not tolerated: ok=%v date=%q tracking=%q", ok, date, tracking)
	}
}

func TestIsTrnOrSubTracking(t *testing.T) {
	yes := []string{
		"proj-EM-TRN-0042",
		"proj-EM-SUB-0001",
		"PROJ-em-trn-0001", // case-fold
		"vendor-sub-9",
		"a-TRN-b-SUB-c", // either qualifies
	}
	no := []string{
		"",
		"proj-EM-MDL-0001",
		"trn-without-dashes",
		"sub-without-dashes",
		"prefixTRN",     // no dashes around TRN
		"-TRN",          // missing trailing dash
		"TRN-",          // missing leading dash
		"proj-TRNX-001", // not exactly -TRN-
	}
	for _, s := range yes {
		if !IsTrnOrSubTracking(s) {
			t.Errorf("IsTrnOrSubTracking(%q) = false, want true", s)
		}
	}
	for _, s := range no {
		if IsTrnOrSubTracking(s) {
			t.Errorf("IsTrnOrSubTracking(%q) = true, want false", s)
		}
	}
}
