package billing_test

import (
	"testing"

	"cloud.o-forge.io/core/oc-lib/models/billing"
	"cloud.o-forge.io/core/oc-lib/models/common/enum"
	"cloud.o-forge.io/core/oc-lib/models/order"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestBill_StoreDraftDefault(t *testing.T) {
	b := &billing.Bill{}
	b.StoreDraftDefault()
	assert.True(t, b.IsDraft)
}

func TestBill_CanDelete_Draft(t *testing.T) {
	b := &billing.Bill{}
	b.IsDraft = true
	assert.True(t, b.CanDelete())
}

func TestBill_CanDelete_NonDraft(t *testing.T) {
	b := &billing.Bill{}
	b.IsDraft = false
	assert.False(t, b.CanDelete())
}

func TestBill_CanUpdate_StatusChange_NonDraft(t *testing.T) {
	b := &billing.Bill{Status: enum.PENDING}
	b.IsDraft = false
	set := &billing.Bill{Status: enum.PAID}
	ok, returned := b.CanUpdate(set)
	assert.True(t, ok)
	assert.Equal(t, enum.PAID, returned.(*billing.Bill).Status)
}

func TestBill_CanUpdate_SameStatus_NonDraft(t *testing.T) {
	b := &billing.Bill{Status: enum.PENDING}
	b.IsDraft = false
	set := &billing.Bill{Status: enum.PENDING}
	ok, _ := b.CanUpdate(set)
	assert.False(t, ok)
}

func TestBill_CanUpdate_Draft(t *testing.T) {
	b := &billing.Bill{Status: enum.PENDING}
	b.IsDraft = true
	set := &billing.Bill{Status: enum.PAID}
	ok, _ := b.CanUpdate(set)
	assert.True(t, ok)
}

func TestBill_GetAccessor(t *testing.T) {
	b := &billing.Bill{}
	acc := b.GetAccessor(&tools.APIRequest{})
	assert.NotNil(t, acc)
}

func TestBill_GetAccessor_NilRequest(t *testing.T) {
	b := &billing.Bill{}
	acc := b.GetAccessor(nil)
	assert.NotNil(t, acc)
}

func TestGenerateBill_Basic(t *testing.T) {
	o := &order.Order{
		AbstractObject: utils.AbstractObject{UUID: "order-uuid-1"},
	}
	req := &tools.APIRequest{PeerID: "peer-abc"}
	b, err := billing.GenerateBill(o, req)
	require.NoError(t, err)
	assert.NotNil(t, b)
	assert.Equal(t, "order-uuid-1", b.OrderID)
	assert.Equal(t, enum.PENDING, b.Status)
	assert.False(t, b.IsDraft)
	assert.Contains(t, b.Name, "peer-abc")
}

func TestBill_SumUpBill_NoSubOrders(t *testing.T) {
	b := &billing.Bill{Total: 0}
	result, err := b.SumUpBill(nil)
	require.NoError(t, err)
	assert.Equal(t, 0.0, result.Total)
}
