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

package template

import (
	"context"
	"fmt"
	"net/http"
	"sort"

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

type PublicPage struct {
	LocalizedAlternates []*LocalizedAlternate
	Menu                *siteMenu
}

func NewPublicPage() *PublicPage {
	return &PublicPage{}
}

func (p *PublicPage) Setup(r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
	schema := httplib.Protocol(r)
	authority := httplib.Host(r)
	_, path := httplib.ShiftPath(r.RequestURI)
	for _, l := range company.Locales {
		p.LocalizedAlternates = append(p.LocalizedAlternates, &LocalizedAlternate{
			Lang:    l.Language.String(),
			Endonym: l.Endonym,
			HRef:    fmt.Sprintf("%s://%s/%s%s", schema, authority, l.Language, path),
		})
	}
	sort.Slice(p.LocalizedAlternates, func(i, j int) bool { return p.LocalizedAlternates[i].Lang < p.LocalizedAlternates[j].Lang })

	p.Menu = &siteMenu{
		CampsiteTypes: mustCollectMenuItems(r.Context(), conn, user.Locale, "select name, '/campsites/types/' || slug from campsite_type where company_id = $1", company.ID),
	}
}

type LocalizedAlternate struct {
	Lang    string
	HRef    string
	Endonym string
}

type siteMenu struct {
	CampsiteTypes []*menuItem
}

type menuItem struct {
	Label string
	HRef  string
}

func mustCollectMenuItems(ctx context.Context, conn *database.Conn, loc *locale.Locale, sql string, args ...interface{}) []*menuItem {
	rows, err := conn.Query(ctx, sql, args...)
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	localePath := "/" + loc.Language.String()
	var items []*menuItem
	for rows.Next() {
		item := &menuItem{}
		err = rows.Scan(&item.Label, &item.HRef)
		if err != nil {
			panic(err)
		}
		item.HRef = localePath + item.HRef
		items = append(items, item)
	}
	if rows.Err() != nil {
		panic(rows.Err())
	}

	return items
}
