package main

import (
	"TaskApp/handlers"
	"TaskApp/httpClient"
	"TaskApp/repository"
	"TaskApp/storage"
	"github.com/gorilla/mux"
	"github.com/joho/godotenv"
	"net/http"
	"os"
	"strconv"
	"time"
)

func main() {
	err := godotenv.Load(".env")
	if err != nil {
		panic("Error loading .env file")
	}

	url := os.Getenv("PERSON_APP_URL")
	port := os.Getenv("PORT")
	databasePath := os.Getenv("DATABASE")
	timeout, err := strconv.Atoi(os.Getenv("TIMEOUT"))

	if err != nil {
		panic("Error converting timeout to int")
	}

	database, err := storage.Init(databasePath)
	if err != nil {
		panic(err)
	}

	cln := httpClient.NewClient(url, time.Duration(timeout))
	rep := repository.NewTaskRepository(database)
	router := mux.NewRouter()
	handlers.InitRoutes(router, rep, cln)

	err = http.ListenAndServe(":"+port, router)
	if err != nil {
		panic(err)
	}

	storage.Close(database)
}