package mission

import "testing"

func TestComputeStatus(t *testing.T) {
	tests := []struct {
		name  string
		forms []FormStatus
		want  string
	}{
		{name: "no completed form is draft", forms: BuildForms("", FormFacts{FormKeyFlightData: StatusDraft}), want: StatusDraft},
		{name: "single in progress form is in progress", forms: BuildForms("", FormFacts{FormKeyFlightData: StatusInProgress}), want: StatusInProgress},
		{name: "all forms complete is complete", forms: BuildForms("", FormFacts{FormKeyFlightData: StatusCompleted}), want: StatusCompleted},
		{name: "partial completion is on progress", forms: []FormStatus{{Key: FormKeyFlightData, Status: StatusCompleted}, {Key: "patient_data", Status: StatusDraft}}, want: StatusInProgress},
		{name: "no forms defaults to draft", forms: nil, want: StatusDraft},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			if got := ComputeStatus(tc.forms); got != tc.want {
				t.Fatalf("ComputeStatus(%+v) = %q, want %q", tc.forms, got, tc.want)
			}
		})
	}
}

func TestBuildForms(t *testing.T) {
	tests := []struct {
		name      string
		missionTy string
		facts     FormFacts
		want      []FormStatus
	}{
		{
			name:      "hems has flight data and patient data",
			missionTy: "HEMS",
			facts:     FormFacts{FormKeyFlightData: StatusCompleted},
			want: []FormStatus{
				{Key: FormKeyFlightData, Status: StatusCompleted},
				{Key: FormKeyPatientData, Status: StatusDraft},
			},
		},
		{
			name:      "cat has flight data only",
			missionTy: "CAT",
			facts:     FormFacts{FormKeyFlightData: StatusDraft},
			want:      []FormStatus{{Key: FormKeyFlightData, Status: StatusDraft}},
		},
		{
			name:      "spo has flight data only",
			missionTy: "SPO",
			facts:     FormFacts{FormKeyFlightData: StatusCompleted},
			want:      []FormStatus{{Key: FormKeyFlightData, Status: StatusCompleted}},
		},
		{
			name:      "nco has flight data only",
			missionTy: "NCO",
			facts:     FormFacts{FormKeyFlightData: StatusDraft},
			want:      []FormStatus{{Key: FormKeyFlightData, Status: StatusDraft}},
		},
		{
			name:      "unknown mission type falls back to provided facts",
			missionTy: "UNKNOWN",
			facts:     FormFacts{FormKeyFlightData: StatusCompleted, FormKeyPatientData: StatusDraft},
			want: []FormStatus{
				{Key: FormKeyFlightData, Status: StatusCompleted},
				{Key: FormKeyPatientData, Status: StatusDraft},
			},
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			forms := BuildForms(tc.missionTy, tc.facts)
			if len(forms) != len(tc.want) {
				t.Fatalf("expected %d forms, got %d", len(tc.want), len(forms))
			}
			for i := range tc.want {
				if forms[i].Key != tc.want[i].Key || forms[i].Status != tc.want[i].Status {
					t.Fatalf("form %d = %+v, want %+v", i, forms[i], tc.want[i])
				}
			}
		})
	}
}

func TestFormStatus(t *testing.T) {
	if got := formStatus(FormKeyFlightData, StatusCompleted); got.Status != StatusCompleted || got.Key != FormKeyFlightData {
		t.Fatalf("unexpected completed form: %+v", got)
	}
	if got := formStatus(FormKeyFlightData, StatusDraft); got.Status != StatusDraft {
		t.Fatalf("unexpected draft form: %+v", got)
	}
}
