/*
 * SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package surroundings

import (
	"context"
	"net/http"
	"strconv"

	"github.com/jackc/pgx/v4"

	"dev.tandem.ws/tandem/camper/pkg/auth"
	"dev.tandem.ws/tandem/camper/pkg/database"
	"dev.tandem.ws/tandem/camper/pkg/form"
	httplib "dev.tandem.ws/tandem/camper/pkg/http"
	"dev.tandem.ws/tandem/camper/pkg/locale"
	"dev.tandem.ws/tandem/camper/pkg/template"
)

type AdminHandler struct {
}

func NewAdminHandler() *AdminHandler {
	return &AdminHandler{}
}

func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var head string
		head, r.URL.Path = httplib.ShiftPath(r.URL.Path)

		switch head {
		case "":
			switch r.Method {
			case http.MethodGet:
				serveHighlightsIndex(w, r, user, company, conn)
			case http.MethodPost:
				addHighlight(w, r, user, company, conn)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPost)
			}
		case "new":
			switch r.Method {
			case http.MethodGet:
				f := newHighlightForm(company)
				f.MustRender(w, r, user, company)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodGet)
			}
		case "order":
			switch r.Method {
			case http.MethodPost:
				orderHighlights(w, r, user, company, conn)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodPost)
			}
		case "ad":
			switch r.Method {
			case http.MethodPut:
				updateAd(w, r, user, company, conn)
			case http.MethodDelete:
				removeAd(w, r, company, conn)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodPut, http.MethodDelete)
			}
		default:
			id, err := strconv.Atoi(head)
			if err != nil {
				http.NotFound(w, r)
				return
			}
			f := newHighlightForm(company)
			if err := f.FillFromDatabase(r.Context(), conn, id); err != nil {
				if database.ErrorIsNotFound(err) {
					http.NotFound(w, r)
					return
				}
				panic(err)
			}

			head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
			switch head {
			case "":
				switch r.Method {
				case http.MethodGet:
					f.MustRender(w, r, user, company)
				case http.MethodPut:
					editHighlight(w, r, user, company, conn, f)
				case http.MethodDelete:
					deleteHighlight(w, r, user, conn, id)
				default:
					httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut, http.MethodDelete)
				}
			default:
				http.NotFound(w, r)
			}
		}
	})
}

func orderHighlights(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	if err := user.VerifyCSRFToken(r); err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}
	input := r.PostForm["highlight_id"]
	if len(input) > 0 {
		var ids []int
		for _, s := range input {
			if id, err := strconv.Atoi(s); err == nil {
				ids = append(ids, id)
			} else {
				http.Error(w, err.Error(), http.StatusUnprocessableEntity)
				return
			}
		}
		if err := conn.OrderSurroundingsHighlights(r.Context(), ids); err != nil {
			panic(err)
		}
	}
	serveHighlightsIndex(w, r, user, company, conn)
}

func serveHighlightsIndex(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	f := newAdForm(company)
	if err := f.FillFromDatabase(r.Context(), company, conn); err != nil && !database.ErrorIsNotFound(err) {
		panic(err)
	}
	serveHighlightsIndexWithForm(w, r, user, company, conn, f)
}

func serveHighlightsIndexWithForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *adForm) {
	highlights, err := collectHighlightEntries(r.Context(), company, conn)
	if err != nil {
		panic(err)
	}
	page := &highlightIndex{
		Ad:         f,
		Highlights: highlights,
	}
	page.MustRender(w, r, user, company)
}

type highlightIndex struct {
	Ad         *adForm
	Highlights []*highlightEntry
}

func (page *highlightIndex) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
	template.MustRenderAdmin(w, r, user, company, "surroundings/index.gohtml", page)
}

type highlightEntry struct {
	ID    int
	URL   string
	Media string
	Name  string
}

func collectHighlightEntries(ctx context.Context, company *auth.Company, conn *database.Conn) ([]*highlightEntry, error) {
	rows, err := conn.Query(ctx, `
		select surroundings_highlight_id
		     , '/admin/surroundings/' || surroundings_highlight_id
		     , media.path
		     , name
		from surroundings_highlight as highlight
		join media using (media_id)
		where highlight.company_id = $1
		order by position
		       , name
	`, company.ID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var highlights []*highlightEntry
	for rows.Next() {
		entry := &highlightEntry{}
		if err = rows.Scan(&entry.ID, &entry.URL, &entry.Media, &entry.Name); err != nil {
			return nil, err
		}
		highlights = append(highlights, entry)
	}

	return highlights, nil
}

func addHighlight(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	f := newHighlightForm(company)
	processHighlightForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
		var err error
		if f.ID, err = tx.AddSurroundingsHighlight(ctx, company.ID, f.Media.Int(), f.Name[f.DefaultLang].Val, f.Description[f.DefaultLang].Val); err != nil {
			return err
		}
		return translateHighlight(ctx, tx, company, f)
	})
}

func processHighlightForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *highlightForm, act func(context.Context, *database.Tx) error) {
	if err := f.Parse(r); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	if err := user.VerifyCSRFToken(r); err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}
	if ok, err := f.Valid(r.Context(), conn, user.Locale); err != nil {
		panic(err)
	} else if !ok {
		if !httplib.IsHTMxRequest(r) {
			w.WriteHeader(http.StatusUnprocessableEntity)
		}
		f.MustRender(w, r, user, company)
		return
	}
	tx := conn.MustBegin(r.Context())
	if err := act(r.Context(), tx); err == nil {
		tx.MustCommit(r.Context())
	} else {
		if rErr := tx.Rollback(r.Context()); rErr != nil {
			err = rErr
		}
		panic(err)
	}
	httplib.Redirect(w, r, "/admin/surroundings", http.StatusSeeOther)
}

func translateHighlight(ctx context.Context, tx *database.Tx, company *auth.Company, f *highlightForm) error {
	for lang := range company.Locales {
		l := lang.String()
		if l == f.DefaultLang {
			continue
		}
		if err := tx.TranslateSurroundingsHighlight(ctx, f.ID, lang, f.Name[l].Val, f.Description[l].Val); err != nil {
			return err
		}
	}
	return nil
}

func editHighlight(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *highlightForm) {
	processHighlightForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
		if err := tx.EditSurroundingsHighlight(ctx, f.ID, f.Media.Int(), f.Name[f.DefaultLang].Val, f.Description[f.DefaultLang].Val); err != nil {
			return err
		}
		return translateHighlight(ctx, tx, company, f)
	})
	httplib.Redirect(w, r, "/admin/surroundings", http.StatusSeeOther)
}

func deleteHighlight(w http.ResponseWriter, r *http.Request, user *auth.User, conn *database.Conn, id int) {
	if err := user.VerifyCSRFToken(r); err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}
	if err := conn.RemoveSurroundingsHighlight(r.Context(), id); err != nil {
		panic(err)
	}
	httplib.Redirect(w, r, "/admin/surroundings", http.StatusSeeOther)
}

type highlightForm struct {
	DefaultLang string
	ID          int
	Media       *form.Media
	Name        form.I18nInput
	Description form.I18nInput
}

func newHighlightForm(company *auth.Company) *highlightForm {
	return &highlightForm{
		DefaultLang: company.DefaultLanguage.String(),
		Media: &form.Media{
			Input: &form.Input{
				Name: "media",
			},
			Label:  locale.PgettextNoop("Highlight image", "input"),
			Prompt: locale.PgettextNoop("Set highlight image", "action"),
		},
		Name:        form.NewI18nInput(company.Locales, "name"),
		Description: form.NewI18nInput(company.Locales, "description"),
	}
}

func (f *highlightForm) FillFromDatabase(ctx context.Context, conn *database.Conn, id int) error {
	f.ID = id
	var name database.RecordArray
	var description database.RecordArray
	row := conn.QueryRow(ctx, `
		select media_id::text
		     , highlight.name
		     , highlight.description::text
		     , array_agg((lang_tag, i18n.name))
		     , array_agg((lang_tag, i18n.description::text))
		from surroundings_highlight as highlight
		left join surroundings_highlight_i18n as i18n using (surroundings_highlight_id)
		where surroundings_highlight_id = $1
		group by media_id::text
		       , highlight.name
		       , highlight.description::text
	`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, id)
	if err := row.Scan(&f.Media.Val, &f.Name[f.DefaultLang].Val, &f.Description[f.DefaultLang].Val, &name, &description); err != nil {
		return err
	}
	if err := f.Name.FillArray(name); err != nil {
		return err
	}
	if err := f.Description.FillArray(description); err != nil {
		return err
	}
	return nil
}

func (f *highlightForm) Parse(r *http.Request) error {
	if err := r.ParseForm(); err != nil {
		return err
	}
	f.Media.FillValue(r)
	f.Name.FillValue(r)
	f.Description.FillValue(r)
	return nil
}

func (f *highlightForm) Valid(ctx context.Context, conn *database.Conn, l *locale.Locale) (bool, error) {
	v := form.NewValidator(l)
	if v.CheckRequired(f.Media.Input, l.GettextNoop("Slide image can not be empty.")) {
		if _, err := v.CheckImageMedia(ctx, conn, f.Media.Input, l.GettextNoop("Slide image must be an image media type.")); err != nil {
			return false, err
		}
	}
	v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty."))
	return v.AllOK, nil
}

func (f *highlightForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
	template.MustRenderAdmin(w, r, user, company, "surroundings/form.gohtml", f)
}

func updateAd(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	f := newAdForm(company)
	if err := f.Parse(r); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	if err := user.VerifyCSRFToken(r); err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}
	if ok, err := f.Valid(r.Context(), conn, user.Locale); err != nil {
		panic(err)
	} else if !ok {
		if !httplib.IsHTMxRequest(r) {
			w.WriteHeader(http.StatusUnprocessableEntity)
		}
		serveHighlightsIndexWithForm(w, r, user, company, conn, f)
		return
	}
	tx := conn.MustBegin(r.Context())
	defer tx.Rollback(r.Context())
	if err := tx.SetupSurroundingsAd(r.Context(), company.ID, f.Media.Int(), f.Title[f.DefaultLang].Val, f.Anchor[f.DefaultLang].Val, f.HRef.Val); err != nil {
		panic(err)
	}
	for lang := range company.Locales {
		l := lang.String()
		if l == f.DefaultLang {
			continue
		}
		if err := tx.TranslateSurroundingsAd(r.Context(), company.ID, lang, f.Title[l].Val, f.Anchor[l].Val); err != nil {
			panic(err)
		}
	}
	tx.MustCommit(r.Context())
	httplib.Redirect(w, r, "/admin/surroundings", http.StatusSeeOther)
}

func removeAd(w http.ResponseWriter, r *http.Request, company *auth.Company, conn *database.Conn) {
	if err := conn.RemoveSurroundingsAd(r.Context(), company.ID); err != nil {
		panic(err)
	}
	httplib.Redirect(w, r, "/admin/surroundings", http.StatusSeeOther)
}

type adForm struct {
	DefaultLang string
	Media       *form.Media
	Title       form.I18nInput
	Anchor      form.I18nInput
	HRef        *form.Input
}

func newAdForm(company *auth.Company) *adForm {
	return &adForm{
		DefaultLang: company.DefaultLanguage.String(),
		Media: &form.Media{
			Input: &form.Input{
				Name: "media",
			},
			Label:  locale.PgettextNoop("Ad image", "input"),
			Prompt: locale.PgettextNoop("Set ad image", "action"),
		},
		Title:  form.NewI18nInput(company.Locales, "title"),
		Anchor: form.NewI18nInput(company.Locales, "anchor"),
		HRef:   &form.Input{Name: "href"},
	}
}

func (f *adForm) FillFromDatabase(ctx context.Context, company *auth.Company, conn *database.Conn) error {
	var titles database.RecordArray
	var anchors database.RecordArray
	err := conn.QueryRow(ctx, `
		select ad.media_id::text
		     , ad.title
		     , ad.anchor
		     , ad.href::text
		     , array_agg((lang_tag, i18n.title))
		     , array_agg((lang_tag, i18n.anchor))
        from surroundings_ad as ad
        left join surroundings_ad_i18n as i18n using (company_id)
    	where company_id = $1
		group by ad.media_id
		       , ad.title
		       , ad.anchor
		       , ad.href
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID).Scan(&f.Media.Val, &f.Title[f.DefaultLang].Val, &f.Anchor[f.DefaultLang].Val, &f.HRef.Val, &titles, &anchors)
	if err != nil {
		return err
	}
	if err := f.Title.FillArray(titles); err != nil {
		return err
	}
	if err := f.Anchor.FillArray(anchors); err != nil {
		return err
	}
	return nil
}

func (f *adForm) Parse(r *http.Request) error {
	if err := r.ParseForm(); err != nil {
		return err
	}
	f.Media.FillValue(r)
	f.Title.FillValue(r)
	f.Anchor.FillValue(r)
	f.HRef.FillValue(r)
	return nil
}

func (f *adForm) Valid(ctx context.Context, conn *database.Conn, l *locale.Locale) (bool, error) {
	v := form.NewValidator(l)
	if v.CheckRequired(f.Media.Input, l.GettextNoop("Ad image can not be empty.")) {
		if _, err := v.CheckImageMedia(ctx, conn, f.Media.Input, l.GettextNoop("Ad image must be an image media type.")); err != nil {
			return false, err
		}
	}
	v.CheckRequired(f.Title[f.DefaultLang], l.GettextNoop("The title can not be empty."))
	v.CheckRequired(f.Anchor[f.DefaultLang], l.GettextNoop("The link text can not be empty."))
	if v.CheckRequired(f.HRef, l.GettextNoop("The ad URL can not be empty")) {
		v.CheckValidURL(f.HRef, l.GettextNoop("This web address is not valid. It should be like https://domain.com/."))
	}
	return v.AllOK, nil
}