package main

import (
	"context"
	"fmt"
	"os"

	"update_server/internal/config"
	"update_server/internal/db"
)

func main() {
	cfg, err := config.Load()
	if err != nil {
		exitWithError(err)
	}

	database, err := db.Open(context.Background(), cfg.SQLitePath)
	if err != nil {
		exitWithError(err)
	}
	defer database.Close()

	if err := db.Migrate(context.Background(), database, cfg.MigrationsDir); err != nil {
		exitWithError(err)
	}

	fmt.Fprintf(os.Stdout, "migrations applied successfully to %s\n", cfg.SQLitePath)
}

func exitWithError(err error) {
	fmt.Fprintf(os.Stderr, "update_server migrate: %v\n", err)
	os.Exit(1)
}
