package main

import (
	"embed"
	"log"
	"mime/multipart"
	"net/http"
	"os"

	"git.hafen.run/lukas/timeshare/handlers"
	"git.hafen.run/lukas/timeshare/pages"
	"github.com/a-h/templ"
	"github.com/valkey-io/valkey-go"
)

//go:embed assets
var assetsFS embed.FS

type File struct {
	FileKey string
	File    *multipart.FileHeader
}

func main() {
	valkeyAddr := os.Getenv("VALKEY_ADDR")
	if valkeyAddr == "" {
		valkeyAddr = "127.0.0.1:6379"
	}
	client, err := valkey.NewClient(valkey.ClientOption{InitAddress: []string{valkeyAddr}})
	if err != nil {
		log.Fatalln("unable to connect to valkey instance: ", err.Error())
	}

	http.Handle("/assets/", http.FileServer(http.FS(assetsFS)))
	http.Handle("GET /upload", templ.Handler(pages.Upload([]pages.Expiry{
		{DurationCode: "24h", DurationName: "24 Hours"},
		{DurationCode: "36h", DurationName: "36 Hours"},
		{DurationCode: "48h", DurationName: "48 Hours"},
		{DurationCode: "168h", DurationName: "1 Week"},
	}, "")))

	http.Handle("POST /upload", http.HandlerFunc(handlers.UploadFiles(client)))
	http.Handle("/download/{upload_id}/zip", http.HandlerFunc(handlers.DownloadFilesZipped(client)))
	http.Handle("/download/{upload_id}/{file_id}", http.HandlerFunc(handlers.DownloadFile(client)))
	http.Handle("/{upload_id}", http.HandlerFunc(handlers.ListFiles(client)))
	http.Handle("/", templ.Handler(pages.Index()))
	log.Println(http.ListenAndServe(":8080", nil))
}
