package mdb_test

import (
	_ "embed"
	"testing"

	"git.sunturtle.xyz/zephyr/horse"
	"git.sunturtle.xyz/zephyr/horse/mdb"
	"github.com/google/go-cmp/cmp"
)

//go:embed testdata/race.sql
var raceSQL string

func TestRaces(t *testing.T) {
	db := testdb(t.Context(), "file:TestRaces?mode=memory&cache=shared", raceSQL)
	got, err := mdb.Races(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Race{
		{
			ID:        1005,
			Name:      "Satsuki Sho",
			Thumbnail: 1005,
			Primary:   1005,
		},
		{
			ID:        1010,
			Name:      "Tokyo Yushun (Japanese Derby)",
			Thumbnail: 1010,
			Primary:   1010,
		},
		{
			ID:        1015,
			Name:      "Kikuka Sho",
			Thumbnail: 1015,
			Primary:   1015,
		},
		{
			ID:        1026,
			Name:      "Kikuka Sho",
			Thumbnail: 1015,
			Primary:   1015,
		},
		{
			ID:        1028,
			Name:      "Satsuki Sho",
			Thumbnail: 1028,
			Primary:   1005,
		},
		{
			ID:        1101,
			Name:      "Teio Sho",
			Thumbnail: 1101,
			Primary:   1101,
		},
		{
			ID:        2001,
			Name:      "Nikkei Shinshun Hai",
			Thumbnail: 2001,
			Primary:   2001,
		},
		{
			ID:        2010,
			Name:      "Spring Stakes",
			Thumbnail: 2010,
			Primary:   2010,
		},
		{
			ID:        2035,
			Name:      "Spring Stakes",
			Thumbnail: 2010,
			Primary:   2010,
		},
		{
			ID:        3001,
			Name:      "Kyoto Kimpai",
			Thumbnail: 3001,
			Primary:   3001,
		},
		{
			ID:        4001,
			Name:      "Manyo Stakes",
			Thumbnail: 4001,
			Primary:   4001,
		},
		{
			ID:        4501,
			Name:      "Aster Sho",
			Thumbnail: 4501,
			Primary:   4501,
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong races (+got/-want):\n%s", diff)
	}
}

func TestSaddles(t *testing.T) {
	db := testdb(t.Context(), "file:TestSaddles?mode=memory&cache=shared", raceSQL)
	got, err := mdb.Saddles(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Saddle{
		{
			ID:      1,
			Name:    "Classic Triple Crown",
			Races:   []horse.RaceID{100501, 101001, 101501},
			Type:    0,
			Primary: 1,
		},
		{
			ID:      12,
			Name:    "Japanese Derby",
			Races:   []horse.RaceID{101001},
			Type:    3,
			Primary: 12,
		},
		{
			ID:      16,
			Name:    "Kikuka Sho",
			Races:   []horse.RaceID{101501},
			Type:    3,
			Primary: 16,
		},
		{
			ID:      18,
			Name:    "Satsuki Sho",
			Races:   []horse.RaceID{100501},
			Type:    3,
			Primary: 18,
		},
		{
			ID:      36,
			Name:    "Teio Sho",
			Races:   []horse.RaceID{110101},
			Type:    3,
			Primary: 36,
		},
		{
			ID:      40,
			Name:    "Nikkei Shinshun Hai",
			Races:   []horse.RaceID{200101},
			Type:    2,
			Primary: 40,
		},
		{
			ID:      49,
			Name:    "Spring S.",
			Races:   []horse.RaceID{201001},
			Type:    2,
			Primary: 49,
		},
		{
			ID:      74,
			Name:    "Kyoto Kimpai",
			Races:   []horse.RaceID{300101},
			Type:    1,
			Primary: 74,
		},
		{
			ID:      144,
			Name:    "Classic Triple Crown",
			Races:   []horse.RaceID{100501, 101001, 102601},
			Type:    0,
			Primary: 1,
		},
		{
			ID:      148,
			Name:    "Kikuka Sho",
			Races:   []horse.RaceID{102601},
			Type:    3,
			Primary: 16,
		},
		{
			ID:      149,
			Name:    "Spring S.",
			Races:   []horse.RaceID{203501},
			Type:    2,
			Primary: 49,
		},
		{
			ID:      154,
			Name:    "Classic Triple Crown",
			Races:   []horse.RaceID{102801, 101001, 101501},
			Type:    0,
			Primary: 1,
		},
		{
			ID:      155,
			Name:    "Satsuki Sho",
			Races:   []horse.RaceID{102801},
			Type:    3,
			Primary: 18,
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong saddles (+got/-want):\n%s", diff)
	}
}

func TestScenarios(t *testing.T) {
	db := testdb(t.Context(), "file:TestScenarios?mode=memory&cache=shared", raceSQL)
	got, err := mdb.Scenarios(t.Context(), db)
	if err != nil {
		t.Error(err)
	}
	want := []horse.Scenario{
		{
			ID:    1,
			Name:  "URA Finale",
			Title: "The Beginning: URA Finale",
		},
		{
			ID:    2,
			Name:  "Unity Cup",
			Title: "Unity Cup: Shine On, Team Spirit!",
		},
		{
			ID:    4,
			Name:  "TS Climax",
			Title: "Trackblazer: Start of the Climax",
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("wrong scenarios (+got/-want):\n%s", diff)
	}
}
