package services

import (
	"context"

	mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
	"git.coopgo.io/coopgo-platform/mobility-accounts/storage"
	"google.golang.org/grpc"
)

type MobilityAccountService struct {
	mobilityaccounts.MobilityAccountsClient
}

func NewMobilityAccountService(mobilityAccountsDial string) (*MobilityAccountService, error) {
	mobilityAccountsConn, err := grpc.Dial(mobilityAccountsDial, grpc.WithInsecure())

	client := mobilityaccounts.NewMobilityAccountsClient(mobilityAccountsConn)
	if err != nil {
		return nil, err
	}

	return &MobilityAccountService{
		MobilityAccountsClient: client,
	}, nil
}

func (s *ServicesHandler) GetBeneficiaries() (accounts []storage.Account, err error) {
	accounts = []storage.Account{}
	request := &mobilityaccounts.GetAccountsRequest{
		Namespaces: []string{"parcoursmob_beneficiaries"},
	}
	resp, err := s.GRPC.MobilityAccounts.GetAccounts(context.TODO(), request)

	if err == nil {
		for _, v := range resp.Accounts {
			a := v.ToStorageType()
			accounts = append(accounts, a)
		}
	}

	return
}

func (s *ServicesHandler) GetAccounts() (accounts []storage.Account, err error) {
	accounts = []storage.Account{}
	request := &mobilityaccounts.GetAccountsRequest{
		Namespaces: []string{"parcoursmob"},
	}
	resp, err := s.GRPC.MobilityAccounts.GetAccounts(context.TODO(), request)

	if err == nil {
		for _, v := range resp.Accounts {
			a := v.ToStorageType()
			accounts = append(accounts, a)
		}
	}

	return
}
