package controller

import (
	"testing"
	"time"

	"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
)

func TestApplyCutOverLayingLookupOverride(t *testing.T) {
	t.Run("marks direct cut-over laying flock as laying", func(t *testing.T) {
		chickinDate := time.Date(2025, time.August, 15, 0, 0, 0, 0, time.UTC)
		result := &dto.ProjectFlockKandangDTO{
			ChickInDate: &chickinDate,
			ProjectFlock: &dto.ProjectFlockWithPivotDTO{
				Category: "LAYING",
			},
		}

		applyCutOverLayingLookupOverride(result)

		if !result.IsLaying {
			t.Fatalf("expected cut-over laying flock to be exposed as laying")
		}
		if result.IsTransition {
			t.Fatalf("expected cut-over laying flock to stay out of transition")
		}
	})

	t.Run("keeps transfer-based state when chickin is absent", func(t *testing.T) {
		result := &dto.ProjectFlockKandangDTO{
			ProjectFlock: &dto.ProjectFlockWithPivotDTO{
				Category: "LAYING",
			},
		}

		applyCutOverLayingLookupOverride(result)

		if result.IsLaying {
			t.Fatalf("expected lookup override to skip non cut-over laying flocks")
		}
	})

	t.Run("does not change already laying lookup state", func(t *testing.T) {
		chickinDate := time.Date(2025, time.August, 15, 0, 0, 0, 0, time.UTC)
		result := &dto.ProjectFlockKandangDTO{
			IsLaying:    true,
			ChickInDate: &chickinDate,
			ProjectFlock: &dto.ProjectFlockWithPivotDTO{
				Category: "LAYING",
			},
		}

		applyCutOverLayingLookupOverride(result)

		if !result.IsLaying {
			t.Fatalf("expected laying state to remain true")
		}
	})
}
