package main

import (
	"context"
	"flag"
	"os"
)

type Config struct {
	Port int
	Root string
	DB   struct {
		Scheme string
		Conn   string
	}
}

func NewConfig(ctx context.Context) (Config, error) {
	var c Config
	fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)

	fs.IntVar(&c.Port, "p", 8080, "port")
	fs.StringVar(&c.Root, "r", "/tmp/", "static file root")
	fs.StringVar(&c.DB.Scheme, "db-scheme", "sqlite", "sql scheme")
	fs.StringVar(&c.DB.Conn, "db-conn", "/tmp/out.sql", "sql conn string")

	if err := fs.Parse(os.Args[1:]); err != nil {
		return Config{}, err
	}

	if _, err := NewDB(ctx, c.DB.Scheme, c.DB.Conn); err != nil {
		return Config{}, err
	}

	return c, nil
}
