// Package client provides a Go SDK for interacting with the MapleFile API.
package client

import (
	"context"
)

// Register creates a new user account.
func (c *Client) Register(ctx context.Context, input *RegisterInput) (*RegisterResponse, error) {
	var resp RegisterResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/register", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// VerifyEmailCode verifies the email verification code.
func (c *Client) VerifyEmailCode(ctx context.Context, input *VerifyEmailInput) (*VerifyEmailResponse, error) {
	var resp VerifyEmailResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/verify-email-code", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// ResendVerification resends the email verification code.
func (c *Client) ResendVerification(ctx context.Context, email string) error {
	input := ResendVerificationInput{Email: email}
	return c.doRequest(ctx, "POST", "/api/v1/resend-verification", input, nil, false)
}

// RequestOTT requests a One-Time Token for login.
func (c *Client) RequestOTT(ctx context.Context, email string) (*OTTResponse, error) {
	input := map[string]string{"email": email}
	var resp OTTResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/request-ott", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// VerifyOTT verifies a One-Time Token and returns the encrypted challenge.
func (c *Client) VerifyOTT(ctx context.Context, email, ott string) (*VerifyOTTResponse, error) {
	input := map[string]string{
		"email": email,
		"ott":   ott,
	}
	var resp VerifyOTTResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/verify-ott", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// CompleteLogin completes the login process with the decrypted challenge.
// On success, the client automatically stores the tokens and calls the OnTokenRefresh callback.
func (c *Client) CompleteLogin(ctx context.Context, input *CompleteLoginInput) (*LoginResponse, error) {
	var resp LoginResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/complete-login", input, &resp, false); err != nil {
		return nil, err
	}

	// Store the tokens
	c.SetTokens(resp.AccessToken, resp.RefreshToken)

	// Notify callback if set, passing the expiry date
	if c.onTokenRefresh != nil {
		c.onTokenRefresh(resp.AccessToken, resp.RefreshToken, resp.AccessTokenExpiryDate)
	}

	return &resp, nil
}

// RefreshToken manually refreshes the access token using the stored refresh token.
// On success, the client automatically updates the stored tokens and calls the OnTokenRefresh callback.
func (c *Client) RefreshToken(ctx context.Context) error {
	return c.refreshAccessToken(ctx)
}

// RecoveryInitiate initiates the account recovery process.
func (c *Client) RecoveryInitiate(ctx context.Context, email, method string) (*RecoveryInitiateResponse, error) {
	input := RecoveryInitiateInput{
		Email:  email,
		Method: method,
	}
	var resp RecoveryInitiateResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/recovery/initiate", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// RecoveryVerify verifies the recovery challenge.
func (c *Client) RecoveryVerify(ctx context.Context, input *RecoveryVerifyInput) (*RecoveryVerifyResponse, error) {
	var resp RecoveryVerifyResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/recovery/verify", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// RecoveryComplete completes the account recovery and resets credentials.
func (c *Client) RecoveryComplete(ctx context.Context, input *RecoveryCompleteInput) (*RecoveryCompleteResponse, error) {
	var resp RecoveryCompleteResponse
	if err := c.doRequest(ctx, "POST", "/api/v1/recovery/complete", input, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}
