package handler

import (
	"context"
	"fmt"
	"solidarity-service/services"
	"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
	"github.com/pkg/errors"
	"github.com/rs/zerolog/log"
	"math/rand"
)

func (h *SolidarityServiceHandler) UpdatePhoneNumber(ctx context.Context, id string, phone_number string) error {
	code := rand.Intn(9999)
	err := h.Services.MobilityAccounts.UpdatePhoneNumber(
		ctx,
		id,
		phone_number,
		false,
		fmt.Sprintf("%04d", code),
	)
	if err != nil {
		log.Error().Err(err).Msg("updating phone number failed")
		return err
	}

	err = h.Services.Push.Send(
		services.Notification{
			Platform:   services.PushToSMSFactor,
			Recipients: []string{phone_number[1:]},
			Title:      "SILVERMOBI",
			Message:    fmt.Sprintf("SILVERMOBI - Votre code de validation : %04d", code),
		},
	)

	if err != nil {
		log.Error().Err(err).Msg("issue sending verification code by sms")
	}

	return nil
}

func (h *SolidarityServiceHandler) VerifyPhoneNumber(ctx context.Context, id string, phone_number string, verification_code string) error {
	request := &grpcapi.GetAccountRequest{
		Id: id,
	}
	resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
	if err != nil {
		return err
	}

	account_ := resp.Account

	log.Debug().
		Str("phone_number", phone_number).
		Str("account.phone_number", account_.Authentication.Local.PhoneNumber).
		Str("verification_code", verification_code).
		Str("account.verification_code", account_.Authentication.Local.PhoneNumberValidation.ValidationCode).
		Msg("Verify phone number")

	if account_.Authentication.Local.PhoneNumber != phone_number || account_.Authentication.Local.PhoneNumberValidation.ValidationCode != verification_code {
		return errors.New("cound not validate phone number : verification code mismatch")
	}

	err = h.Services.MobilityAccounts.UpdatePhoneNumber(
		ctx,
		id,
		phone_number,
		true,
		"",
	)
	if err != nil {
		return err
	}

	return nil
}

func (h *SolidarityServiceHandler) UpdateBirthDate(ctx context.Context, id string, birthdate string) error {
	err := h.Services.MobilityAccounts.UpdateAccountBirthDate(ctx, id, "silvermobi", birthdate)
	if err != nil {
		return err
	}
	return nil
}

func (h *SolidarityServiceHandler) SetAccountType(ctx context.Context, id string, accountType string) error {
	err := h.Services.MobilityAccounts.SetAccountType(ctx, id, accountType)
	if err != nil {
		return err
	}
	return nil
}
func (h *SolidarityServiceHandler) GetAccountType(ctx context.Context, id string) (account_type string, err error) {
	request := &grpcapi.GetAccountRequest{
		Id: id,
	}
	resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
	if err != nil {
		return "", err
		log.Error().Err(err).Msg("Failed get account type")
	}
	account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
	if account.Type != "" {

		return account.Type, nil
	}
	return "", errors.New("account type not set")
}
func (h *SolidarityServiceHandler) GetAccountPhone(ctx context.Context, id string) (phone_number string, err error) {
	request := &grpcapi.GetAccountRequest{
		Id: id,
	}
	resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
	if err != nil {
		return "", err
		log.Error().Err(err).Msg("Failed get account type")
	}
	account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
	if account.PhoneNumber != "" {

		return account.PhoneNumber, nil
	}
	return "", errors.New("invalid request ")
}

func (h *SolidarityServiceHandler) CheckValidation(ctx context.Context, id string) (phone_validation, birth_validation, type_validation bool, err error) {
	request := &grpcapi.GetAccountRequest{
		Id: id,
	}
	resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
	if err != nil {
		return false, false, false, err
		log.Error().Err(err).Msg("Failed get validation response")
	}
	account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
	phone_validation = false
	birth_validation = false
	type_validation = false
	if account.LocalCredentials.PhoneNumberVerified {
		phone_validation = true
	}
	if account.BirthDate != "" {
		birth_validation = true
	}
	if account.Type != "" {
		type_validation = true
	}

	log.Info().Msg("Getting phone and birth validation for " + account.Email)
	return phone_validation, birth_validation, type_validation, nil
}
