package rmo

import (
	"net/http"

	"github.com/Gleipnir-Technology/nidus-sync/db"
	"github.com/Gleipnir-Technology/nidus-sync/db/models"
	"github.com/Gleipnir-Technology/nidus-sync/html"
	"github.com/Gleipnir-Technology/nidus-sync/platform/email"
	"github.com/aarondl/opt/omit"
	"github.com/go-chi/chi/v5"
)

type ContentEmail struct {
	Email string
}

func getEmailByCode(w http.ResponseWriter, r *http.Request) {
	id := chi.URLParam(r, "code")
	//id := r.FormValue("id")
	if id == "" {
		http.Error(w, "You must specify an id", http.StatusBadRequest)
		return
	}
	ctx := r.Context()
	email_log, err := models.CommsEmailLogs.Query(
		models.SelectWhere.CommsEmailLogs.PublicID.EQ(id),
	).One(ctx, db.PGInstance.BobDB)
	if err != nil {
		respondError(w, "Failed to query email_log: %w", err, http.StatusInternalServerError)
		return
	}
	html, err := email.RenderHTML(email_log.TemplateID, email_log.TemplateData)
	if err != nil {
		respondError(w, "Failed to render email_log: %w", err, http.StatusInternalServerError)
		return
	}
	w.Write(html)
}
func getEmailReportUnsubscribe(w http.ResponseWriter, r *http.Request) {
	email := r.FormValue("email")
	html.RenderOrError(
		w,
		"rmo/email-unsubscribe.html",
		ContentEmail{
			Email: email,
		},
	)
}
func getEmailConfirm(w http.ResponseWriter, r *http.Request) {
	email := r.FormValue("email")
	if email == "" {
		respondError(w, "Not sure what to do with an empty email", nil, http.StatusBadRequest)
		return
	}

	html.RenderOrError(
		w,
		"rmo/email-confirm.html",
		ContentEmail{
			Email: email,
		},
	)
}
func getEmailConfirmComplete(w http.ResponseWriter, r *http.Request) {
	html.RenderOrError(
		w,
		"rmo/email-confirm-complete.html",
		map[string]string{},
	)
}
func getEmailUnsubscribe(w http.ResponseWriter, r *http.Request) {
	email := r.FormValue("email")
	html.RenderOrError(
		w,
		"rmo/email-unsubscribe.html",
		ContentEmail{
			Email: email,
		},
	)
}
func getEmailUnsubscribeComplete(w http.ResponseWriter, r *http.Request) {
	html.RenderOrError(
		w,
		"rmo/email-unsubscribe-complete.html",
		map[string]string{},
	)
}
func postEmailConfirm(w http.ResponseWriter, r *http.Request) {
	email := r.PostFormValue("email")
	if email == "" {
		respondError(w, "Not sure what to do with an empty email", nil, http.StatusBadRequest)
		return
	}
	ctx := r.Context()
	email_contact, err := models.FindCommsEmailContact(ctx, db.PGInstance.BobDB, email)
	if err != nil {
		respondError(w, "Email not in the database", err, http.StatusNotFound)
		return
	}
	err = email_contact.Update(ctx, db.PGInstance.BobDB, &models.CommsEmailContactSetter{
		Confirmed: omit.From(true),
	})
	http.Redirect(w, r, "/email/confirm/complete", http.StatusFound)
}
func postEmailUnsubscribe(w http.ResponseWriter, r *http.Request) {
	email := r.PostFormValue("email")
	if email == "" {
		respondError(w, "Not sure what to do with an empty email", nil, http.StatusBadRequest)
		return
	}
	ctx := r.Context()
	email_contact, err := models.FindCommsEmailContact(ctx, db.PGInstance.BobDB, email)
	if err != nil {
		respondError(w, "Email not in the database", err, http.StatusNotFound)
		return
	}
	err = email_contact.Update(ctx, db.PGInstance.BobDB, &models.CommsEmailContactSetter{
		IsSubscribed: omit.From(false),
	})
	http.Redirect(w, r, "/email/unsubscribe/complete", http.StatusFound)
}
