package store

import (
	"context"
	"database/sql"
	"errors"
	"reflect"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/AgentTeam/internal/coredb"
	"github.com/tidbcloud/AgentTeam/internal/store/storeerr"
)

func TestGraphHasNoMethods(t *testing.T) {
	graphType := reflect.TypeOf((*Graph)(nil))
	require.Equal(t, 1, graphType.NumMethod())
	require.Equal(t, "Validate", graphType.Method(0).Name)
}

func TestGraphWiresChannelWorkspaceAccess(t *testing.T) {
	graph := New(graphFakeDB{}, nil, nil, 30)

	_, err := graph.Channel.ListChannelsForUser(context.Background(), "usr_missing", "ws_missing")

	require.ErrorIs(t, err, storeerr.ErrForbidden)
}

type graphFakeDB struct{}

func (graphFakeDB) QueryRow(context.Context, string, ...any) coredb.Row {
	return graphFakeRow{err: sql.ErrNoRows}
}

func (graphFakeDB) Query(context.Context, string, ...any) (coredb.Rows, error) {
	return nil, errors.New("unexpected graphFakeDB.Query call")
}

func (graphFakeDB) Exec(context.Context, string, ...any) (coredb.Result, error) {
	return nil, errors.New("unexpected graphFakeDB.Exec call")
}

func (graphFakeDB) BeginTx(context.Context, coredb.TxOptions) (coredb.Tx, error) {
	return nil, errors.New("unexpected graphFakeDB.BeginTx call")
}

type graphFakeRow struct {
	err error
}

func (row graphFakeRow) Scan(...any) error {
	return row.err
}
