/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package maindb

import (
	"context"
	"fmt"
	"testing"
	"time"

	"mstore/pkg/auxtool"
	"mstore/pkg/auxuuid"
	"mstore/pkg/descr"

	"github.com/stretchr/testify/require"
)

func TestGrant(t *testing.T) {
	var err error

	dbDir := t.TempDir()
	db := NewDatabase(dbDir)

	err = db.OpenDatabase()
	require.NoError(t, err)

	err = db.InitDatabase()
	require.NoError(t, err)

	id := auxuuid.NewUUID()
	accountID := auxuuid.NewUUID()
	timenow := auxtool.TimeNow()
	creator := auxuuid.NewUUID()
	newGrant := &descr.Grant{
		ID:        id,
		AccountID: accountID,
		Right:     "rigthFoo",
		Pattern:   `*`,
		CreatedAt: timenow,
		UpdatedAt: timenow,
		CreatedBy: creator,
		UpdatedBy: creator,
	}
	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)

	err = db.InsertGrant(ctx, newGrant)
	require.NoError(t, err)

	files, err := db.ListGrantsByAccountID(ctx, accountID)
	require.NoError(t, err)
	require.Equal(t, len(files), 1)
	require.Equal(t, files[0].ID, id)
	require.Equal(t, files[0].AccountID, accountID)
	require.Equal(t, files[0].CreatedBy, creator)
	fmt.Println(files[0].CreatedBy)

}
