package runs

import (
	"errors"
	"testing"

	"textmachine/platform/internal/money"
)

// The UPPER half of the ceiling re-judgement in Start. The doc comment there calls it load-bearing —
// "the number that decides how much money is reserved cannot be one the caller chose unilaterally" —
// and the contract says max_chapters is already clamped to what is left of the book, so a client that
// asks for more than the scale allows must be refused and no money may move.
//
// The fixture is the one that makes the difference visible: an account that can afford 333 chapters
// and a book that has THREE. bounds.Max is 3, so a request for 100 is out of bounds by the BOOK and
// not by the balance, which is the half `if in.CeilingChapters < bounds.Min` alone cannot see.
func TestR1ACeilingAboveTheScaleIsRefusedAndTakesNoMoney(t *testing.T) {
	f := newFixture(t, "10", 3)
	_, err := f.svc.Start(f.ctx, StartRequest{UserID: "u1", BookID: f.bookID(t), CeilingChapters: 100})
	if !errors.Is(err, ErrCeilingOutOfBounds) {
		t.Fatalf("a ceiling of 100 chapters on a 3-chapter book: err = %v, want ErrCeilingOutOfBounds", err)
	}
	a := f.account(t)
	if a.Reserved != 0 {
		t.Errorf("a hold of %s was taken for a refused run", a.Reserved.USD())
	}
	if want := money.MicroUSD(10_000_000); a.Balance != want {
		t.Errorf("balance %s after a refused run, want %s", a.Balance.USD(), want.USD())
	}
}
