package main

import (
	"database/sql"
	"embed"
	"fmt"
	"log"

	_ "github.com/mattn/go-sqlite3"
	"github.com/pressly/goose/v3"
)

//go:embed migrations/*.sql
var embedMigrations embed.FS

func main() {
	// Connect to database
	db, err := sql.Open("sqlite3", ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Set up goose with embedded migrations
	goose.SetBaseFS(embedMigrations)

	if err := goose.SetDialect("sqlite3"); err != nil {
		log.Fatal(err)
	}

	// Run all pending migrations
	if err := goose.Up(db, "migrations"); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Migrations applied successfully")

	// Check current version
	version, err := goose.GetDBVersion(db)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Current DB version:", version)

	// Roll back one migration
	if err := goose.Down(db, "migrations"); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Rolled back one migration")

	// Migrate to specific version
	if err := goose.UpTo(db, "migrations", 1); err != nil {
		log.Fatal(err)
	}
	fmt.Println("Migrated to version 1")

	// Get migration status
	fmt.Println("\nMigration status:")
	if err := goose.Status(db, "migrations"); err != nil {
		log.Fatal(err)
	}
}

// Goose also supports Go migrations for complex logic:
//
// func init() {
//     goose.AddMigration(upAddUsers, downAddUsers)
// }
//
// func upAddUsers(tx *sql.Tx) error {
//     _, err := tx.Exec(`CREATE TABLE users (id INTEGER PRIMARY KEY)`)
//     return err
// }
//
// func downAddUsers(tx *sql.Tx) error {
//     _, err := tx.Exec(`DROP TABLE users`)
//     return err
// }
