package mdb

import (
	"context"
	_ "embed"

	"zombiezen.com/go/sqlite"
	"zombiezen.com/go/sqlite/sqlitex"

	"git.sunturtle.xyz/zephyr/horse"
)

var (
	//go:embed sql/race.sql
	raceSQL string
	//go:embed sql/saddle.sql
	saddleSQL string
	//go:embed sql/scenario.sql
	scenarioSQL string
)

// Races retrieves all races.
func Races(ctx context.Context, db *sqlitex.Pool) ([]horse.Race, error) {
	return load(ctx, db, nil, raceSQL, func(s *sqlite.Stmt) horse.Race {
		return horse.Race{
			ID:   horse.RaceID(s.ColumnInt(0)),
			Name: s.ColumnText(1),
			// TODO(zeph): grade
			Thumbnail: s.ColumnInt(3),
			Primary:   horse.RaceID(s.ColumnInt(4)),
		}
	})
}

// Saddles retrieves all saddles.
func Saddles(ctx context.Context, db *sqlitex.Pool) ([]horse.Saddle, error) {
	return load(ctx, db, nil, saddleSQL, func(s *sqlite.Stmt) horse.Saddle {
		return horse.Saddle{
			ID:   horse.SaddleID(s.ColumnInt(0)),
			Name: s.ColumnText(1),
			Races: trimZeros(
				horse.RaceID(s.ColumnInt(2)),
				horse.RaceID(s.ColumnInt(3)),
				horse.RaceID(s.ColumnInt(4)),
			),
			Type:    horse.SaddleType(s.ColumnInt(5)),
			Primary: horse.SaddleID(s.ColumnInt(6)),
		}
	})
}

// Scenarios retrieves all scenarios.
func Scenarios(ctx context.Context, db *sqlitex.Pool) ([]horse.Scenario, error) {
	return load(ctx, db, nil, scenarioSQL, func(s *sqlite.Stmt) horse.Scenario {
		return horse.Scenario{
			ID:    horse.ScenarioID(s.ColumnInt(0)),
			Name:  s.ColumnText(1),
			Title: s.ColumnText(2),
		}
	})
}
