package main

import (
	"flag"
	"fmt"
	"log"
	"net/netip"
	"os"
	"os/signal"
	"path"
	"strings"
	"syscall"
	"time"

	"git.arav.su/Arav/dwelling-home/internal/http"
	mfsqlite "git.arav.su/Arav/dwelling-home/pkg/mindflow/database/sqlite"
	gb "git.arav.su/Arav/justguestbook"
)

var version string

var showVersion *bool = flag.Bool("v", false, "show version")
var listenAddress *string = flag.String("l", "/var/run/dwelling-home/sock", "listen address (ip:port|unix_path)")
var captchaExpiry *time.Duration = flag.Duration("ce", 10*time.Minute, "CAPTCHA expiry (e.g. 5m, 60s)")
var guestbookOwner *string = flag.String("gbo", "Admin", "name of a guestbook owner")
var guestbookPageSize *int64 = flag.Int64("gbp", 60, "size of a guestbook page")
var databasesPath *string = flag.String("db", "/var/lib/dwelling-home", "path to a directory where to store DB files")

func main() {
	flag.Parse()

	if *showVersion {
		fmt.Println("dwelling-home Ver.", version, "\nCopyright (c) 2023 Alexander \"Arav\" Andreev <me@arav.su>")
		return
	}

	var network string
	if !strings.ContainsRune(*listenAddress, ':') {
		network = "unix"
		defer os.Remove(*listenAddress)
	} else {
		ap, err := netip.ParseAddrPort(*listenAddress)
		if err != nil {
			log.Fatalln(err)
		} else if !ap.IsValid() {
			log.Fatalln(*listenAddress, "is not valid")
		}

		if ap.Addr().Is4() {
			network = "tcp4"
		} else if ap.Addr().Is6() {
			network = "tcp6"
		}
	}

	guestbookDB, err := gb.NewSQLiteDB(path.Join(*databasesPath, "guestbook.sqlite"))
	if err != nil {
		log.Fatalln(err)
	}
	defer guestbookDB.Close()

	mindflowDB, err := mfsqlite.New(path.Join(*databasesPath, "mindflow.sqlite"))
	if err != nil {
		log.Fatalln(err)
	}
	defer mindflowDB.Close()

	hand := http.NewHandlers(*captchaExpiry, *guestbookOwner, *guestbookPageSize, guestbookDB, mindflowDB)
	defer hand.CloseDB()

	srv := http.NewHttpServer()

	srv.SetNotFoundHandler(http.NotFound)

	srv.ServeStatic("/assets/*filepath", hand.AssetsFS())
	srv.GET("/", hand.Index)
	srv.GET("/stuff", hand.Stuff)
	srv.GET("/stuff/article/*filepath", hand.Article)
	srv.GET("/mindflow", hand.Mindflow)
	srv.GET("/mindflow/admin", hand.MindflowAdmin)
	srv.GET("/about", hand.About)
	srv.GET("/guestbook", hand.Guestbook)
	srv.GET("/guestbook/admin", hand.GuestbookAdmin)

	srv.GET("/robots.txt", hand.Robots)
	srv.GET("/rss.xml", hand.RSS)
	srv.GET("/sitemap.xml", hand.Sitemap)

	captchaApi := http.NewCaptchaApiHandlers(*captchaExpiry)

	srv.POST("/api/captcha/", captchaApi.New)
	srv.POST("/api/captcha/:id", captchaApi.Solve)
	srv.GET("/api/captcha/:id/image", captchaApi.Image)

	guestbookApi := http.NewGuestbookApiHandlers(*guestbookOwner, *guestbookPageSize, guestbookDB)

	srv.POST("/api/guestbook", guestbookApi.New)
	srv.PATCH("/api/guestbook/:id", guestbookApi.Edit)
	srv.DELETE("/api/guestbook/:id", guestbookApi.Delete)
	srv.POST("/api/guestbook/:id/reply", guestbookApi.Reply)
	srv.PATCH("/api/guestbook/:id/reply", guestbookApi.EditReply)
	srv.DELETE("/api/guestbook/:id/reply", guestbookApi.DeleteReply)

	mindflowApi := http.NewMindflowApiHandlers(mindflowDB)

	srv.POST("/api/mindflow", mindflowApi.New)
	srv.POST("/api/mindflow/:id", mindflowApi.Edit)
	srv.DELETE("/api/mindflow/:id", mindflowApi.Delete)

	if err := srv.Start(network, *listenAddress); err != nil {
		log.Fatalln(err)
	}

	doneSignal := make(chan os.Signal, 1)
	signal.Notify(doneSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

	<-doneSignal

	if err := srv.Stop(); err != nil {
		log.Fatalln(err)
	}
}
