package service

import (
	"strings"
	"testing"
)

func TestValidateTransferLayingUsableRouteRules(t *testing.T) {
	t.Run("valid header rule", func(t *testing.T) {
		flagGroup, err := validateTransferLayingUsableRouteRules([]transferLayingUsableRouteRule{
			{FlagGroupCode: "AYAM", SourceTable: transferLayingUsableSourceTable},
		}, 10)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if flagGroup != "AYAM" {
			t.Fatalf("unexpected flag group: %s", flagGroup)
		}
	})

	t.Run("missing usable header rule", func(t *testing.T) {
		_, err := validateTransferLayingUsableRouteRules(nil, 10)
		if err == nil {
			t.Fatal("expected error")
		}
		if !strings.Contains(strings.ToLower(err.Error()), "tidak ditemukan") {
			t.Fatalf("unexpected error: %v", err)
		}
	})

	t.Run("legacy rule still active", func(t *testing.T) {
		_, err := validateTransferLayingUsableRouteRules([]transferLayingUsableRouteRule{
			{FlagGroupCode: "AYAM", SourceTable: transferLayingUsableSourceTable},
			{FlagGroupCode: "AYAM", SourceTable: transferLayingLegacyUsableSourceTable},
		}, 10)
		if err == nil {
			t.Fatal("expected error")
		}
		if !strings.Contains(strings.ToLower(err.Error()), "legacy") {
			t.Fatalf("unexpected error: %v", err)
		}
	})

	t.Run("ambiguous active header rules", func(t *testing.T) {
		_, err := validateTransferLayingUsableRouteRules([]transferLayingUsableRouteRule{
			{FlagGroupCode: "AYAM", SourceTable: transferLayingUsableSourceTable},
			{FlagGroupCode: "PAKAN", SourceTable: transferLayingUsableSourceTable},
		}, 10)
		if err == nil {
			t.Fatal("expected error")
		}
		if !strings.Contains(strings.ToLower(err.Error()), "ambigu") {
			t.Fatalf("unexpected error: %v", err)
		}
	})
}
