package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/handlers"
	"github.com/jinzhu/gorm"

	"gogs.carducci-dante.gov.it/karmen/config"
	"gogs.carducci-dante.gov.it/karmen/core/api"
	"gogs.carducci-dante.gov.it/karmen/core/renderer"
	"gogs.carducci-dante.gov.it/karmen/orm"
)

func regenerateRelations(db *gorm.DB) error {
	var activities []orm.Activity

	if err := db.Find(&activities).Error; err != nil {
		return err
	}

	defer func() {
		if err := recover(); err != nil {
			log.Print(err)
		}
	}()

	for _, a := range activities {
		err := db.Save(&a).Error
		if err != nil {
			panic(err)
		}
	}

	return nil
}

func main() {
	log.Println("Loading config file...")
	err := config.ReadFile("config/config.yaml", config.Config)
	if err != nil {
		panic(err)
	}

	log.Println("Starting karmen...")

	db, err := orm.New(fmt.Sprintf("%s?%s", config.Config.Orm.Connection, config.Config.Orm.Options))
	if err != nil {
		panic(err)
	}

	orm.Use(db)

	if config.Config.Orm.Reset {
		log.Print("Resetting the DB...")
		orm.Reset()
	}

	if config.Config.Orm.AutoMigrate {
		log.Print("Automigrating...")
		orm.AutoMigrate()
	}

	if config.Config.Orm.Regenerate {
		log.Print("Regenerate relations...")
		if err := regenerateRelations(db); err != nil {
			panic(err)
		}
	}

	rend, err := renderer.NewHTMLRenderer("templates/")
	if err != nil {
		panic(err)
	}

	renderer.Use(rend)

	log.Println("karmen is listening to port 3000...")
	if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, api.Handlers())); err != nil {
		panic(err)
	}
}
