package purchase_resource_test

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

func TestGetAccessor(t *testing.T) {
	req := &tools.APIRequest{}
	res := &purchase_resource.PurchaseResource{}
	accessor := res.GetAccessor(req)

	assert.NotNil(t, accessor)
	assert.Equal(t, tools.PURCHASE_RESOURCE, accessor.(*purchase_resource.PurchaseResourceMongoAccessor).Type)
}

func TestCanUpdate(t *testing.T) {
	set := &purchase_resource.PurchaseResource{ResourceID: "id"}
	r := &purchase_resource.PurchaseResource{
		AbstractObject: utils.AbstractObject{IsDraft: true},
	}
	can, updated := r.CanUpdate(set)
	assert.True(t, can)
	assert.Equal(t, set, updated)

	r.IsDraft = false
	can, _ = r.CanUpdate(set)
	assert.False(t, can)
}

func TestCanDelete(t *testing.T) {
	now := time.Now().UTC()
	past := now.Add(-5 * time.Minute)
	future := now.Add(5 * time.Minute)

	t.Run("nil EndDate", func(t *testing.T) {
		r := &purchase_resource.PurchaseResource{}
		assert.False(t, r.CanDelete())
	})

	t.Run("EndDate in past", func(t *testing.T) {
		r := &purchase_resource.PurchaseResource{EndDate: &past}
		assert.True(t, r.CanDelete())
	})

	t.Run("EndDate in future", func(t *testing.T) {
		r := &purchase_resource.PurchaseResource{EndDate: &future}
		assert.False(t, r.CanDelete())
	})
}
