package flightdata

import (
	"testing"
	"time"

	missiondomain "wucher/internal/domain/mission"
)

func TestIsComplete(t *testing.T) {
	if IsComplete(nil) {
		t.Fatal("expected nil row to be incomplete")
	}
	if IsComplete(&FlightData{Status: StatusInProgress}) {
		t.Fatal("expected on_progress status to be incomplete")
	}
	if !IsComplete(&FlightData{Status: StatusCompleted}) {
		t.Fatal("expected complete status to be complete")
	}
}

func TestDeriveStatus(t *testing.T) {
	now := time.Now().UTC()
	base := &FlightData{
		MissionID:          []byte("1234567890123456"),
		FromICAOID:         []byte("3456789012345678"),
		ToHospitalID:       []byte("4567890123456789"),
		FlightTakeOff:      now,
		FlightLanding:      now,
		FlightDuration:     time.Hour,
		LandingCount:       1,
		DeliveryNoteNumber: "DN-1",
		CustomerName:       "Customer",
	}

	t.Run("cat complete", func(t *testing.T) {
		base.Mission = &missiondomain.Mission{Type: "CAT"}
		if got := DeriveStatus(base, nil); got != StatusCompleted {
			t.Fatalf("expected complete, got %q", got)
		}
	})

	t.Run("nco complete without rotor brake cycle", func(t *testing.T) {
		base.Mission = &missiondomain.Mission{Type: "NCO"}
		if got := DeriveStatus(base, nil); got != StatusCompleted {
			t.Fatalf("expected complete, got %q", got)
		}
	})

	t.Run("hems requires rotor brake cycle", func(t *testing.T) {
		base.Mission = &missiondomain.Mission{Type: "HEMS"}
		if got := DeriveStatus(base, nil); got != StatusInProgress {
			t.Fatalf("expected on_progress, got %q", got)
		}
		base.RotorBrakeCycle = 1
		if got := DeriveStatus(base, nil); got != StatusCompleted {
			t.Fatalf("expected complete after rotor brake cycle is set, got %q", got)
		}
		base.RotorBrakeCycle = 0
	})

	t.Run("spo complete without details", func(t *testing.T) {
		base.Mission = &missiondomain.Mission{Type: "SPO"}
		if got := DeriveStatus(base, nil); got != StatusCompleted {
			t.Fatalf("expected complete, got %q", got)
		}
	})

	t.Run("spo complete with details", func(t *testing.T) {
		base.Mission = &missiondomain.Mission{Type: "SPO"}
		details := &SPODetails{
			HESLO: SPOHESLODetails{
				Flights: []SPOHESLOFlight{{
					ROT:            1,
					FuelTruckID:    []byte("5678901234567890"),
					LoggingSlingID: []byte("6789012345678901"),
				}},
				Slings: []SPOHESLOSling{{
					HESLOFlightID: []byte("5678901234567890"),
					SlingID:       []byte("7890123456789012"),
				}},
			},
			Logging: SPOLoggingDetails{
				Slings: []SPOLoggingSling{{
					SlingID: []byte("8901234567890123"),
				}},
			},
			HEC: SPOHECDetails{
				Flights: []SPOHECFlight{{
					ROT:         1,
					HECCycle:    1,
					EquipmentID: []byte("9012345678901234"),
				}},
				Slings: []SPOHECSling{{
					SlingID: []byte("0123456789012345"),
				}},
				Loads: []SPOHECLoad{{
					LoadCategory: "LT200",
					Quantity:     1,
				}},
			},
		}
		if got := DeriveStatus(base, details); got != StatusCompleted {
			t.Fatalf("expected complete, got %q", got)
		}
	})

	t.Run("spo missing section still complete", func(t *testing.T) {
		base.Mission = &missiondomain.Mission{Type: "SPO"}
		details := &SPODetails{
			HESLO: SPOHESLODetails{
				Flights: []SPOHESLOFlight{{
					ROT:            1,
					FuelTruckID:    []byte("5678901234567890"),
					LoggingSlingID: []byte("6789012345678901"),
				}},
			},
		}
		if got := DeriveStatus(base, details); got != StatusCompleted {
			t.Fatalf("expected complete, got %q", got)
		}
	})
}
