// Code generated by cmd/genapi. DO NOT EDIT.

//go:build !ignore_autogenerated

package api

import (
	"bytes"
	"context"
	"errors"
	"io"
	"net/http"
	"strings"
	"testing"

	"github.com/lukaszraczylo/go-telegram/client"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

// genTestMockDoer is a testify-mock HTTPDoer used by generated tests only.
type genTestMockDoer struct{ mock.Mock }

func (m *genTestMockDoer) Do(r *http.Request) (*http.Response, error) {
	args := m.Called(r)
	if v := args.Get(0); v != nil {
		return v.(*http.Response), args.Error(1)
	}
	return nil, args.Error(1)
}

func genTestResp(status int, body string) *http.Response {
	return &http.Response{
		StatusCode: status,
		Body:       io.NopCloser(bytes.NewBufferString(body)),
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
}

func Test_GetMe_Success(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
		return strings.HasSuffix(r.URL.Path, "/getMe")
	})).Return(genTestResp(200, `{"ok":true,"result":{}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.NoError(t, err)
}

func Test_GetMe_APIError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":429,"description":"Too Many Requests","parameters":{"retry_after":1}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 429, ae.Code)
	require.True(t, ae.IsRetryable())
}

func Test_GetMe_NetworkError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, errors.New("dial tcp: timeout"))

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.Error(t, err)
	var ne *client.NetworkError
	require.ErrorAs(t, err, &ne)
}

func Test_GetMe_ParseError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(genTestResp(200, `not json`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.Error(t, err)
	var pe *client.ParseError
	require.ErrorAs(t, err, &pe)
}

func Test_GetMe_ContextCanceled(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, context.Canceled).Maybe()

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(ctx, bot, &GetMeParams{})
	require.Error(t, err)
	require.ErrorIs(t, err, context.Canceled)
}

// Test_GetMe_MissingRequiredFields exercises Telegram's server-side
// validation: when a required field is omitted, Telegram returns 400 with
// a description like "Bad Request: <field> is empty". The library must
// surface this as *APIError with the ErrBadRequest sentinel.
func Test_GetMe_MissingRequiredFields(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	// Send a Params with all required fields zeroed — simulates a caller
	// that forgot to populate them. The bot library marshals as-is and
	// surfaces Telegram's 400 reply.
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 400, ae.Code)
	require.True(t, errors.Is(err, client.ErrBadRequest))
	require.False(t, ae.IsRetryable())
}

// Test_GetMe_Forbidden exercises the 403 path (bot blocked by user,
// removed from chat, etc.). The library must surface the ErrForbidden
// sentinel.
func Test_GetMe_Forbidden(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":403,"description":"Forbidden: bot was blocked by the user"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 403, ae.Code)
	require.True(t, errors.Is(err, client.ErrForbidden))
	require.False(t, ae.IsRetryable())
}

// Test_GetMe_ServerError exercises the 5xx path. The library must
// classify these as retryable so RetryDoer / user retry logic kicks in.
func Test_GetMe_ServerError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":500,"description":"Internal server error"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	_, err := GetMe(context.Background(), bot, &GetMeParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 500, ae.Code)
	require.True(t, ae.IsRetryable(), "5xx must be retryable")
}

func Test_SendMessage_Success(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
		return strings.HasSuffix(r.URL.Path, "/sendMessage")
	})).Return(genTestResp(200, `{"ok":true,"result":{}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(context.Background(), bot, params)
	require.NoError(t, err)
}

func Test_SendMessage_APIError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":429,"description":"Too Many Requests","parameters":{"retry_after":1}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 429, ae.Code)
	require.True(t, ae.IsRetryable())
}

func Test_SendMessage_NetworkError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, errors.New("dial tcp: timeout"))

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(context.Background(), bot, params)
	require.Error(t, err)
	var ne *client.NetworkError
	require.ErrorAs(t, err, &ne)
}

func Test_SendMessage_ParseError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(genTestResp(200, `not json`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(context.Background(), bot, params)
	require.Error(t, err)
	var pe *client.ParseError
	require.ErrorAs(t, err, &pe)
}

func Test_SendMessage_ContextCanceled(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, context.Canceled).Maybe()

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(ctx, bot, params)
	require.Error(t, err)
	require.ErrorIs(t, err, context.Canceled)
}

// Test_SendMessage_MissingRequiredFields exercises Telegram's server-side
// validation: when a required field is omitted, Telegram returns 400 with
// a description like "Bad Request: <field> is empty". The library must
// surface this as *APIError with the ErrBadRequest sentinel.
func Test_SendMessage_MissingRequiredFields(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	// Send a Params with all required fields zeroed — simulates a caller
	// that forgot to populate them. The bot library marshals as-is and
	// surfaces Telegram's 400 reply.
	_, err := SendMessage(context.Background(), bot, &SendMessageParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 400, ae.Code)
	require.True(t, errors.Is(err, client.ErrBadRequest))
	require.False(t, ae.IsRetryable())
}

// Test_SendMessage_Forbidden exercises the 403 path (bot blocked by user,
// removed from chat, etc.). The library must surface the ErrForbidden
// sentinel.
func Test_SendMessage_Forbidden(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":403,"description":"Forbidden: bot was blocked by the user"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 403, ae.Code)
	require.True(t, errors.Is(err, client.ErrForbidden))
	require.False(t, ae.IsRetryable())
}

// Test_SendMessage_ServerError exercises the 5xx path. The library must
// classify these as retryable so RetryDoer / user retry logic kicks in.
func Test_SendMessage_ServerError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":500,"description":"Internal server error"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendMessageParams{
		ChatID: ChatIDFromInt(123),
		Text:   "test_value",
	}
	_, err := SendMessage(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 500, ae.Code)
	require.True(t, ae.IsRetryable(), "5xx must be retryable")
}

func Test_SendDocument_Success(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
		return strings.HasSuffix(r.URL.Path, "/sendDocument")
	})).Return(genTestResp(200, `{"ok":true,"result":{}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(context.Background(), bot, params)
	require.NoError(t, err)
}

func Test_SendDocument_APIError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":429,"description":"Too Many Requests","parameters":{"retry_after":1}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 429, ae.Code)
	require.True(t, ae.IsRetryable())
}

func Test_SendDocument_NetworkError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, errors.New("dial tcp: timeout"))

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(context.Background(), bot, params)
	require.Error(t, err)
	var ne *client.NetworkError
	require.ErrorAs(t, err, &ne)
}

func Test_SendDocument_ParseError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(genTestResp(200, `not json`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(context.Background(), bot, params)
	require.Error(t, err)
	var pe *client.ParseError
	require.ErrorAs(t, err, &pe)
}

func Test_SendDocument_ContextCanceled(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, context.Canceled).Maybe()

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(ctx, bot, params)
	require.Error(t, err)
	require.ErrorIs(t, err, context.Canceled)
}

// Test_SendDocument_MissingRequiredFields exercises Telegram's server-side
// validation: when a required field is omitted, Telegram returns 400 with
// a description like "Bad Request: <field> is empty". The library must
// surface this as *APIError with the ErrBadRequest sentinel.
func Test_SendDocument_MissingRequiredFields(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	// Send a Params with all required fields zeroed — simulates a caller
	// that forgot to populate them. The bot library marshals as-is and
	// surfaces Telegram's 400 reply.
	_, err := SendDocument(context.Background(), bot, &SendDocumentParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 400, ae.Code)
	require.True(t, errors.Is(err, client.ErrBadRequest))
	require.False(t, ae.IsRetryable())
}

// Test_SendDocument_Forbidden exercises the 403 path (bot blocked by user,
// removed from chat, etc.). The library must surface the ErrForbidden
// sentinel.
func Test_SendDocument_Forbidden(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":403,"description":"Forbidden: bot was blocked by the user"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 403, ae.Code)
	require.True(t, errors.Is(err, client.ErrForbidden))
	require.False(t, ae.IsRetryable())
}

// Test_SendDocument_ServerError exercises the 5xx path. The library must
// classify these as retryable so RetryDoer / user retry logic kicks in.
func Test_SendDocument_ServerError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":500,"description":"Internal server error"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &SendDocumentParams{
		ChatID:   42,
		Document: &InputFile{PathOrID: "file_id_test"},
	}
	_, err := SendDocument(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 500, ae.Code)
	require.True(t, ae.IsRetryable(), "5xx must be retryable")
}

func Test_GetUpdates_Success(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
		return strings.HasSuffix(r.URL.Path, "/getUpdates")
	})).Return(genTestResp(200, `{"ok":true,"result":[]}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(context.Background(), bot, params)
	require.NoError(t, err)
}

func Test_GetUpdates_APIError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":429,"description":"Too Many Requests","parameters":{"retry_after":1}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 429, ae.Code)
	require.True(t, ae.IsRetryable())
}

func Test_GetUpdates_NetworkError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, errors.New("dial tcp: timeout"))

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(context.Background(), bot, params)
	require.Error(t, err)
	var ne *client.NetworkError
	require.ErrorAs(t, err, &ne)
}

func Test_GetUpdates_ParseError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(genTestResp(200, `not json`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(context.Background(), bot, params)
	require.Error(t, err)
	var pe *client.ParseError
	require.ErrorAs(t, err, &pe)
}

func Test_GetUpdates_ContextCanceled(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, context.Canceled).Maybe()

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(ctx, bot, params)
	require.Error(t, err)
	require.ErrorIs(t, err, context.Canceled)
}

// Test_GetUpdates_MissingRequiredFields exercises Telegram's server-side
// validation: when a required field is omitted, Telegram returns 400 with
// a description like "Bad Request: <field> is empty". The library must
// surface this as *APIError with the ErrBadRequest sentinel.
func Test_GetUpdates_MissingRequiredFields(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	// Send a Params with all required fields zeroed — simulates a caller
	// that forgot to populate them. The bot library marshals as-is and
	// surfaces Telegram's 400 reply.
	_, err := GetUpdates(context.Background(), bot, &GetUpdatesParams{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 400, ae.Code)
	require.True(t, errors.Is(err, client.ErrBadRequest))
	require.False(t, ae.IsRetryable())
}

// Test_GetUpdates_Forbidden exercises the 403 path (bot blocked by user,
// removed from chat, etc.). The library must surface the ErrForbidden
// sentinel.
func Test_GetUpdates_Forbidden(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":403,"description":"Forbidden: bot was blocked by the user"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 403, ae.Code)
	require.True(t, errors.Is(err, client.ErrForbidden))
	require.False(t, ae.IsRetryable())
}

// Test_GetUpdates_ServerError exercises the 5xx path. The library must
// classify these as retryable so RetryDoer / user retry logic kicks in.
func Test_GetUpdates_ServerError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":500,"description":"Internal server error"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	params := &GetUpdatesParams{}
	_, err := GetUpdates(context.Background(), bot, params)
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 500, ae.Code)
	require.True(t, ae.IsRetryable(), "5xx must be retryable")
}
