package account

import (
	"context"

	pb "git.gocasts.ir/ebhomengo/niki/contract/goprotobuf/account"
	"git.gocasts.ir/ebhomengo/niki/driverapp/service"
	"git.gocasts.ir/ebhomengo/niki/pkg/types"

	"google.golang.org/grpc"
)

type Client struct {
	Conn *grpc.ClientConn
}

func New(conn *grpc.ClientConn) *Client {
	return &Client{
		Conn: conn,
	}
}

func (c Client) SendOTP(ctx context.Context, phoneNumber string) error {

	client := pb.NewAccountServiceClient(c.Conn)

	_, err := client.SendOtp(ctx, &pb.SendOtpRequest{
		PhoneNumber: phoneNumber,
	})
	if err != nil {
		return err
	}

	return nil
}

func (c Client) LoginOrRegister(ctx context.Context, req service.LoginOrRegisterRequest) (service.LoginOrRegisterResponse, error) {

	client := pb.NewAccountServiceClient(c.Conn)

	res, err := client.LoginOrRegister(ctx, &pb.LoginOrRegisterRequest{
		PhoneNumber: req.PhoneNumber,
		VerifyCode:  req.VerifyCode,
	})
	if err != nil {
		return service.LoginOrRegisterResponse{}, err
	}

	return service.LoginOrRegisterResponse{
		ID:          types.ID(res.Id),
		PhoneNumber: res.PhoneNumber,
	}, nil
}
