package handler

import (
	"context"
	"time"
)

func (h *SolidarityServiceHandler) Login(ctx context.Context, username string, password string) (jwt string, err error) {
	account, err := h.Services.MobilityAccounts.Login(ctx, username, password, "silvermobi")
	if err != nil {
		return
	}
	key := h.Config.GetString("identification.local.jwt_secret")
	ttl := h.Config.GetString("identification.local.ttl")
	parsedttl, err := time.ParseDuration(ttl)
	if err != nil {
		return "", err
	}
	return account.CreateToken(parsedttl, key)
}

func (h *SolidarityServiceHandler) Register(ctx context.Context, username string, password string, email string, phone_number string, first_name string, last_name string) (jwt string, err error) {
	account, err := h.Services.MobilityAccounts.Register(
		ctx,
		username,
		password,
		email,
		phone_number,
		map[string]any{
			"first_name":   first_name,
			"last_name":    last_name,
			"email":        email,
			"phone_number": phone_number,
		},
		"silvermobi",
	)

	if err != nil {
		return "", err
	}

	key := h.Config.GetString("identification.local.jwt_secret")
	ttl := h.Config.GetString("identification.local.ttl")

	parsedttl, err := time.ParseDuration(ttl)
	if err != nil {
		return "", err
	}

	return account.CreateToken(parsedttl, key)
}
