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

import (
	"context"
	"fmt"
	"net/url"
)

// GetMe returns the current authenticated user's profile.
func (c *Client) GetMe(ctx context.Context) (*User, error) {
	var resp User
	if err := c.doRequest(ctx, "GET", "/api/v1/me", nil, &resp, true); err != nil {
		return nil, err
	}
	return &resp, nil
}

// UpdateMe updates the current user's profile.
func (c *Client) UpdateMe(ctx context.Context, input *UpdateUserInput) (*User, error) {
	var resp User
	if err := c.doRequest(ctx, "PUT", "/api/v1/me", input, &resp, true); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteMe deletes the current user's account.
func (c *Client) DeleteMe(ctx context.Context, password string) error {
	input := DeleteUserInput{Password: password}
	return c.doRequest(ctx, "DELETE", "/api/v1/me", input, nil, true)
}

// PublicUserLookup looks up a user by email (returns public info only).
// This endpoint does not require authentication.
func (c *Client) PublicUserLookup(ctx context.Context, email string) (*PublicUser, error) {
	path := fmt.Sprintf("/iam/api/v1/users/lookup?email=%s", url.QueryEscape(email))
	var resp PublicUser
	if err := c.doRequest(ctx, "GET", path, nil, &resp, false); err != nil {
		return nil, err
	}
	return &resp, nil
}

// CreateBlockedEmail adds an email to the blocked list.
func (c *Client) CreateBlockedEmail(ctx context.Context, email, reason string) (*BlockedEmail, error) {
	input := CreateBlockedEmailInput{
		Email:  email,
		Reason: reason,
	}
	var resp BlockedEmail
	if err := c.doRequest(ctx, "POST", "/api/v1/me/blocked-emails", input, &resp, true); err != nil {
		return nil, err
	}
	return &resp, nil
}

// ListBlockedEmails returns all blocked emails for the current user.
func (c *Client) ListBlockedEmails(ctx context.Context) (*ListBlockedEmailsResponse, error) {
	var resp ListBlockedEmailsResponse
	if err := c.doRequest(ctx, "GET", "/api/v1/me/blocked-emails", nil, &resp, true); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteBlockedEmail removes an email from the blocked list.
func (c *Client) DeleteBlockedEmail(ctx context.Context, email string) (*DeleteBlockedEmailResponse, error) {
	path := fmt.Sprintf("/api/v1/me/blocked-emails/%s", url.PathEscape(email))
	var resp DeleteBlockedEmailResponse
	if err := c.doRequest(ctx, "DELETE", path, nil, &resp, true); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetDashboard returns the user's dashboard data.
func (c *Client) GetDashboard(ctx context.Context) (*DashboardResponse, error) {
	var resp DashboardResponse
	if err := c.doRequest(ctx, "GET", "/api/v1/dashboard", nil, &resp, true); err != nil {
		return nil, err
	}
	return &resp, nil
}
