package roomturnoverreward

import (
	"context"
	"testing"
	"time"

	"google.golang.org/grpc"
	roomv1 "hyapp.local/api/proto/room/v1"
	walletv1 "hyapp.local/api/proto/wallet/v1"
	domain "hyapp/services/activity-service/internal/domain/roomturnoverreward"
)

func TestWeekWindowUsesUTCMondayBoundary(t *testing.T) {
	cases := []struct {
		name      string
		now       string
		wantStart string
		wantEnd   string
	}{
		{
			name:      "monday zero belongs to new week",
			now:       "2026-06-01T00:00:00Z",
			wantStart: "2026-06-01T00:00:00Z",
			wantEnd:   "2026-06-08T00:00:00Z",
		},
		{
			name:      "sunday belongs to current week",
			now:       "2026-06-07T23:59:59Z",
			wantStart: "2026-06-01T00:00:00Z",
			wantEnd:   "2026-06-08T00:00:00Z",
		},
		{
			name:      "cross month keeps utc week",
			now:       "2026-07-01T12:30:00Z",
			wantStart: "2026-06-29T00:00:00Z",
			wantEnd:   "2026-07-06T00:00:00Z",
		},
		{
			name:      "cross year keeps utc week",
			now:       "2027-01-01T08:00:00Z",
			wantStart: "2026-12-28T00:00:00Z",
			wantEnd:   "2027-01-04T00:00:00Z",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			now := mustParseTime(t, tc.now)
			start, end := WeekWindow(now)
			if got := start.Format(time.RFC3339); got != tc.wantStart {
				t.Fatalf("unexpected start: got %s want %s", got, tc.wantStart)
			}
			if got := end.Format(time.RFC3339); got != tc.wantEnd {
				t.Fatalf("unexpected end: got %s want %s", got, tc.wantEnd)
			}
		})
	}
}

func TestPreviousWeekWindowReturnsCompletedUTCWeek(t *testing.T) {
	start, end := PreviousWeekWindow(mustParseTime(t, "2026-06-08T00:00:00Z"))
	if got := start.Format(time.RFC3339); got != "2026-06-01T00:00:00Z" {
		t.Fatalf("unexpected previous start: %s", got)
	}
	if got := end.Format(time.RFC3339); got != "2026-06-08T00:00:00Z" {
		t.Fatalf("unexpected previous end: %s", got)
	}
}

func TestMatchHighestTierReturnsHighestThreshold(t *testing.T) {
	tiers := []domain.Tier{
		{TierID: 1, TierCode: "bronze", ThresholdCoinSpent: 100, RewardCoinAmount: 10, Status: domain.TierStatusActive},
		{TierID: 2, TierCode: "silver", ThresholdCoinSpent: 500, RewardCoinAmount: 60, Status: domain.TierStatusActive},
		{TierID: 3, TierCode: "gold", ThresholdCoinSpent: 1000, RewardCoinAmount: 160, Status: domain.TierStatusActive},
		{TierID: 4, TierCode: "hidden", ThresholdCoinSpent: 700, RewardCoinAmount: 90, Status: domain.TierStatusInactive},
	}

	tier, ok := MatchHighestTier(tiers, 850)
	if !ok {
		t.Fatal("expected a matched tier")
	}
	if tier.TierCode != "silver" {
		t.Fatalf("unexpected tier: got %s want silver", tier.TierCode)
	}
}

func TestRetrySettlementRejectsTierNotMatchedWithoutGrantSideEffects(t *testing.T) {
	repo := &retryBoundaryRepository{
		settlement: domain.Settlement{
			SettlementID:     "settlement-tier-not-matched",
			RoomID:           "room-1",
			Status:           domain.SettlementStatusFailed,
			FailureReason:    "tier_not_matched",
			WalletCommandID:  "room_turnover_reward:lalu:1:room-1",
			RewardCoinAmount: 0,
		},
	}
	room := &retryBoundaryRoom{ownerID: 10001}
	wallet := &retryBoundaryWallet{}
	svc := New(repo, wallet, room)

	got, err := svc.RetrySettlement(context.Background(), repo.settlement.SettlementID)
	if err == nil {
		t.Fatal("expected retry to reject tier_not_matched settlement")
	}
	if got.Status != domain.SettlementStatusFailed || got.FailureReason != "tier_not_matched" {
		t.Fatalf("retry should return original failed settlement, got %+v", got)
	}
	if repo.resetCalls != 0 || repo.grantCalls != 0 || repo.failCalls != 0 {
		t.Fatalf("tier_not_matched retry must not mutate settlement: reset=%d grant=%d fail=%d", repo.resetCalls, repo.grantCalls, repo.failCalls)
	}
	if room.calls != 0 {
		t.Fatalf("tier_not_matched retry must not fetch room owner, calls=%d", room.calls)
	}
	if wallet.calls != 0 {
		t.Fatalf("tier_not_matched retry must not call wallet, calls=%d", wallet.calls)
	}
}

func mustParseTime(t *testing.T, value string) time.Time {
	t.Helper()
	parsed, err := time.Parse(time.RFC3339, value)
	if err != nil {
		t.Fatalf("parse time %q: %v", value, err)
	}
	return parsed
}

type retryBoundaryRepository struct {
	settlement domain.Settlement
	resetCalls int
	grantCalls int
	failCalls  int
}

func (r *retryBoundaryRepository) GetRoomTurnoverRewardConfig(context.Context) (domain.Config, bool, error) {
	panic("unexpected GetRoomTurnoverRewardConfig")
}

func (r *retryBoundaryRepository) UpdateRoomTurnoverRewardConfig(context.Context, domain.Config, int64) (domain.Config, error) {
	panic("unexpected UpdateRoomTurnoverRewardConfig")
}

func (r *retryBoundaryRepository) ConsumeRoomTurnoverGiftEvent(context.Context, domain.RoomGiftEvent, int64, int64, int64) (domain.EventResult, error) {
	panic("unexpected ConsumeRoomTurnoverGiftEvent")
}

func (r *retryBoundaryRepository) GetRoomTurnoverRewardProgress(context.Context, string, int64) (domain.Progress, bool, error) {
	panic("unexpected GetRoomTurnoverRewardProgress")
}

func (r *retryBoundaryRepository) GetLatestRoomTurnoverRewardSettlement(context.Context, string) (domain.Settlement, bool, error) {
	panic("unexpected GetLatestRoomTurnoverRewardSettlement")
}

func (r *retryBoundaryRepository) ListRoomTurnoverRewardSettlements(context.Context, domain.SettlementQuery) ([]domain.Settlement, int64, error) {
	panic("unexpected ListRoomTurnoverRewardSettlements")
}

func (r *retryBoundaryRepository) ListUnsettledRoomTurnoverRewardProgress(context.Context, int64, int) ([]domain.Progress, error) {
	panic("unexpected ListUnsettledRoomTurnoverRewardProgress")
}

func (r *retryBoundaryRepository) InsertRoomTurnoverRewardSettlement(context.Context, domain.Settlement, int64) (domain.Settlement, bool, error) {
	panic("unexpected InsertRoomTurnoverRewardSettlement")
}

func (r *retryBoundaryRepository) ListPendingRoomTurnoverRewardSettlements(context.Context, int) ([]domain.Settlement, error) {
	panic("unexpected ListPendingRoomTurnoverRewardSettlements")
}

func (r *retryBoundaryRepository) GetRoomTurnoverRewardSettlement(_ context.Context, settlementID string) (domain.Settlement, bool, error) {
	if settlementID != r.settlement.SettlementID {
		return domain.Settlement{}, false, nil
	}
	return r.settlement, true, nil
}

func (r *retryBoundaryRepository) ResetRoomTurnoverRewardSettlementForRetry(_ context.Context, _ string, ownerUserID int64, _ int64) (domain.Settlement, error) {
	r.resetCalls++
	r.settlement.Status = domain.SettlementStatusPending
	r.settlement.OwnerUserID = ownerUserID
	r.settlement.FailureReason = ""
	return r.settlement, nil
}

func (r *retryBoundaryRepository) MarkRoomTurnoverRewardSettlementGranted(_ context.Context, _ string, walletTransactionID string, _ int64) (domain.Settlement, error) {
	r.grantCalls++
	r.settlement.Status = domain.SettlementStatusGranted
	r.settlement.WalletTransactionID = walletTransactionID
	return r.settlement, nil
}

func (r *retryBoundaryRepository) MarkRoomTurnoverRewardSettlementFailed(_ context.Context, _ string, reason string, _ int64) error {
	r.failCalls++
	r.settlement.Status = domain.SettlementStatusFailed
	r.settlement.FailureReason = reason
	return nil
}

type retryBoundaryRoom struct {
	ownerID int64
	calls   int
}

func (r *retryBoundaryRoom) AdminGetRoom(context.Context, *roomv1.AdminGetRoomRequest, ...grpc.CallOption) (*roomv1.AdminGetRoomResponse, error) {
	r.calls++
	return &roomv1.AdminGetRoomResponse{Room: &roomv1.AdminRoomListItem{OwnerUserId: r.ownerID}}, nil
}

type retryBoundaryWallet struct {
	calls int
}

func (w *retryBoundaryWallet) CreditRoomTurnoverReward(context.Context, *walletv1.CreditRoomTurnoverRewardRequest, ...grpc.CallOption) (*walletv1.CreditRoomTurnoverRewardResponse, error) {
	w.calls++
	return &walletv1.CreditRoomTurnoverRewardResponse{TransactionId: "wallet-tx-1", GrantedAtMs: 1234}, nil
}
