package schema

import (
	"context"
	"log"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/uptrace/bun"
)

func TestSelectSports(t *testing.T) {
	var sports []Sport
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&sports).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 4, len(sports))
}

func TestSelectTeams(t *testing.T) {
	var teams []Team
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&teams).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 122, len(teams))
}

func TestSelectFieldPositions(t *testing.T) {
	var positions []FieldPosition
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&positions).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 44, len(positions))
}

func TestSelectPlayers(t *testing.T) {
	var players []Player
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&players).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 38, len(players))
}

func TestSelectRoosters(t *testing.T) {
	var roosters []Rooster
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&roosters).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 11, len(roosters))
}

func TestSelectVendors(t *testing.T) {
	var vendors []Vendor
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&vendors).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 9, len(vendors))
}

func TestSelectCardSets(t *testing.T) {
	var cardSets []CardSet
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&cardSets).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 15, len(cardSets))
}

func TestSelectCards(t *testing.T) {
	var cards []Card
	var err error
	var db *bun.DB
	ctx := context.Background()

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

	err = db.NewSelect().Model(&cards).Scan(ctx)
	if err != nil {
		log.Fatal(err)
	}

	require.NoError(t, err)
	assert.Equal(t, 10, len(cards))
}
