package main

import (
	"fmt"
	"log"
	"os"

	"github.com/golang-migrate/migrate/v4"
	_ "github.com/golang-migrate/migrate/v4/database/postgres"
	_ "github.com/golang-migrate/migrate/v4/source/file"
	_ "github.com/lib/pq"
	"gitlab.ost.ch/marketminds/marketminds/cmd"
)

func main() {
	databaseUrl := os.Getenv("DATABASE_URL")

	// get migration path from env, default to file://db/migrations
	migrationPath := os.Getenv("MIGRATION_PATH")
	if migrationPath == "" {
		migrationPath = "file://db/migrations"
	}

	fmt.Println("Database URL: ", databaseUrl)
	fmt.Println("File URL: ", migrationPath)

	m, err := migrate.New(migrationPath, databaseUrl)
	if err != nil {
		fmt.Println("Error creating migration: ", err)
	}

	if m == nil {
		fmt.Println("Migration is not created")
	} else {
		err = m.Up()
		if err != nil && err != migrate.ErrNoChange {
			log.Fatalf("Could not apply migrations: %v\n", err)
		} else if err == migrate.ErrNoChange {
			fmt.Println("No migrations to apply")
		} else {
			fmt.Println("Migrations applied successfully")
		}
	}

	cmd.Execute()

}
