package legal

import (
	"context"
	"net/http"

	"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:
				serveLegalIndex(w, r, user, company, conn)
			case http.MethodPost:
				addLegal(w, r, user, company, conn)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPost)
			}
		case "new":
			switch r.Method {
			case http.MethodGet:
				f := newLegalForm(company)
				f.MustRender(w, r, user, company)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodGet)
			}
		default:
			f := newLegalForm(company)
			if err := f.FillFromDatabase(r.Context(), conn, company, head); err != nil {
				if database.ErrorIsNotFound(err) {
					http.NotFound(w, r)
					return
				}
				panic(err)
			}
			h.legalHandler(user, company, conn, f).ServeHTTP(w, r)
		}
	})
}

func (h *AdminHandler) legalHandler(user *auth.User, company *auth.Company, conn *database.Conn, f *legalForm) 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:
				f.MustRender(w, r, user, company)
			case http.MethodPut:
				editLegal(w, r, user, company, conn, f)
			default:
				httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
			}
		default:
			http.NotFound(w, r)
		}
	})
}

func serveLegalIndex(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	legals, err := collectLegalEntries(r.Context(), conn, company)
	if err != nil {
		panic(err)
	}
	page := &legalIndex{
		Texts: legals,
	}
	page.MustRender(w, r, user, company)
}

func collectLegalEntries(ctx context.Context, conn *database.Conn, company *auth.Company) ([]*legalEntry, error) {
	rows, err := conn.Query(ctx, `
		select '/admin/legal/' || slug
		     , name
		from legal_text
		where company_id = $1
		order by name
	`, company.ID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var legals []*legalEntry
	for rows.Next() {
		legal := &legalEntry{}
		if err = rows.Scan(&legal.URL, &legal.Name); err != nil {
			return nil, err
		}
		legals = append(legals, legal)
	}

	return legals, nil
}

type legalEntry struct {
	URL  string
	Name string
}

type legalIndex struct {
	Texts []*legalEntry
}

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

func addLegal(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	f := newLegalForm(company)
	processLegalForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
		_, err := tx.Exec(ctx, `
			insert into legal_text (company_id, slug, name, content)
			values ($1, $2, $3, xmlparse(content $4))
		`, company.ID, f.Slug.Val, f.Name[f.DefaultLang].Val, f.Content[f.DefaultLang])
		if err != nil {
			return err
		}
		return translateLegal(ctx, tx, company, f)
	})
}

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

func editLegal(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *legalForm) {
	processLegalForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
		_, err := tx.Exec(ctx, `
			update legal_text
			set name = $3
			  , content = xmlparse(content $4)
			where company_id = $1
			  and slug = $2
		`, company.ID, f.Slug.Val, f.Name[f.DefaultLang].Val, f.Content[f.DefaultLang])
		if err != nil {
			return err
		}
		return translateLegal(ctx, tx, company, f)
	})
}

func processLegalForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *legalForm, act func(ctx context.Context, tx *database.Tx) error) {
	if ok, err := form.Handle(f, w, r, user); err != nil {
		return
	} else if !ok {
		f.MustRender(w, r, user, company)
		return
	}

	tx := conn.MustBegin(r.Context())
	if err := act(r.Context(), tx); err == nil {
		if err := tx.Commit(r.Context()); err != nil {
			panic(err)
		}
	} else {
		if err := tx.Rollback(r.Context()); err != nil {
			panic(err)
		}
		panic(err)
	}
	httplib.Redirect(w, r, "/admin/legal/"+f.Slug.Val, http.StatusSeeOther)
}

type legalForm struct {
	DefaultLang string
	URL         string
	Slug        *form.Input
	Name        form.I18nInput
	Content     form.I18nInput
}

func newLegalForm(company *auth.Company) *legalForm {
	f := &legalForm{
		DefaultLang: company.DefaultLanguage.String(),
		Slug: &form.Input{
			Name: "slug",
		},
		Name:    form.NewI18nInput(company.Locales, "name"),
		Content: form.NewI18nInput(company.Locales, "content"),
	}
	return f
}

func (f *legalForm) FillFromDatabase(ctx context.Context, conn *database.Conn, company *auth.Company, slug string) error {
	var name database.RecordArray
	var content database.RecordArray
	row := conn.QueryRow(ctx, `
		select '/admin/legal/' || text.slug
		     , text.slug
		     , text.name
		     , text.content::text
		     , array_agg((lang_tag, i18n.name))
		     , array_agg((lang_tag, i18n.content::text))
		from legal_text as text
        left join legal_text_i18n as i18n using (company_id, slug)
		where text.company_id = $1
		  and text.slug = $2
		group by text.slug
		       , text.name
		       , text.content::text
	`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID, slug)
	if err := row.Scan(&f.URL, &f.Slug.Val, &f.Name[f.DefaultLang].Val, &f.Content[f.DefaultLang].Val, &name, &content); err != nil {
		return err
	}
	if err := f.Name.FillArray(name); err != nil {
		return err
	}
	if err := f.Content.FillArray(content); err != nil {
		return err
	}
	return nil
}

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

func (f *legalForm) Valid(l *locale.Locale) bool {
	v := form.NewValidator(l)
	if v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty.")) {
		v.CheckMinLength(f.Name[f.DefaultLang], 1, l.GettextNoop("Name must have at least one letter."))
	}
	return v.AllOK
}

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