package main

import (
	// "encoding/json"
	"fmt"
	"net/http"
	"time"
	// "github.com/julienschmidt/httprouter"
	// "strconv"
	// "errors"
	// "party.at/party/internal/data"
	// "party.at/party/internal/validator"
)

func ws(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		fmt.Println("Upgrade error:", err)
		return
	}
	defer conn.Close()

	done := make(chan struct{})

	go func() {
		ticker := time.NewTicker(1 * time.Second)

		for {
			select {
			case <-done:
				return
			case t := <-ticker.C:
				msg := map[string]interface{}{
					"type":      "server_tick",
					"timestamp": t.Format(time.RFC3339),
				}

				if err := conn.WriteJSON(msg); err != nil {
					fmt.Println("Write error:", err)
					return
				}
			}
		}
	}()

	for {
		var msg Message
		err := conn.ReadJSON(&msg)
		if err != nil {
			fmt.Println("Read error:", err)
			break
		}
		fmt.Println(msg)

		// Send a response
		// response := fmt.Sprintf("Server time: %s", time.Now())
		// if err := conn.WriteMessage(websocket.TextMessage, []byte(response)); err != nil {
		// 	fmt.Println("Write error:", err)
		// 	break
		// }
	}
}

// func handleMobileLogin(w http.ResponseWriter, r *http.Request) {
//     // 1. Get the token from the request header
//     rawIDToken := r.Header.Get("Authorization")

//     // 2. Initialize the verifier (pointing to ID Austria's keys)
//     verifier := provider.Verifier(&oidc.Config{ClientID: "YOUR_APP_ID"})

//     // 3. Verify the signature and expiration
//     idToken, err := verifier.Verify(ctx, rawIDToken)
//     if err != nil {
//         http.Error(w, "Invalid Token", http.StatusUnauthorized)
//         return
//     }

//     // 4. Extract User Data
//     var claims struct {
//         Subject string `json:"sub"` // This is the unique ID
//         Name    string `json:"name"`
//     }
//     if err := idToken.Claims(&claims); err != nil {
//         // Handle error
//     }

//     // 5. Create your own application session for the mobile app
//     issueLocalSession(w, claims.Subject)
// }

func redirectToHTTPS(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, "https://localhost:8443"+r.URL.RequestURI(), http.StatusMovedPermanently)
}
