package geminicli

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestTierInfo_UnmarshalJSON(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		wantID   string
		wantErr  bool
	}{
		{"string_tier", `"free-tier"`, "free-tier", false},
		{"object_tier", `{"id":"pro-tier"}`, "pro-tier", false},
		{"empty_string", `""`, "", false},
		{"null", `null`, "", false},
		{"invalid_json_empty", ``, "", true},
		{"invalid_json_whitespace", `   `, "", true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var tier TierInfo
			err := json.Unmarshal([]byte(tt.input), &tier)
			if tt.wantErr {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
				require.Equal(t, tt.wantID, tier.ID)
			}
		})
	}
}

func TestLoadCodeAssistResponse_GetTier(t *testing.T) {
	tests := []struct {
		name string
		resp LoadCodeAssistResponse
		want string
	}{
		{
			name: "paid_tier_priority",
			resp: LoadCodeAssistResponse{
				PaidTier:    &TierInfo{ID: "pro"},
				CurrentTier: &TierInfo{ID: "free"},
			},
			want: "pro",
		},
		{
			name: "current_tier_only",
			resp: LoadCodeAssistResponse{
				CurrentTier: &TierInfo{ID: "free"},
			},
			want: "free",
		},
		{
			name: "paid_empty_fallback",
			resp: LoadCodeAssistResponse{
				PaidTier:    &TierInfo{ID: ""},
				CurrentTier: &TierInfo{ID: "free"},
			},
			want: "free",
		},
		{
			name: "no_tier",
			resp: LoadCodeAssistResponse{},
			want: "",
		},
		{
			name: "nil_tiers",
			resp: LoadCodeAssistResponse{
				PaidTier:    nil,
				CurrentTier: nil,
			},
			want: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := tt.resp.GetTier()
			require.Equal(t, tt.want, got)
		})
	}
}

func TestAllowedTier_Fields(t *testing.T) {
	tier := AllowedTier{
		ID:        "pro",
		IsDefault: true,
	}
	require.Equal(t, "pro", tier.ID)
	require.True(t, tier.IsDefault)
}

func TestOnboardUserRequest_Fields(t *testing.T) {
	req := OnboardUserRequest{
		TierID: "pro",
		Metadata: LoadCodeAssistMetadata{
			IDEType:    "vscode",
			Platform:   "linux",
			PluginType: "extension",
		},
	}
	require.Equal(t, "pro", req.TierID)
	require.Equal(t, "vscode", req.Metadata.IDEType)
	require.Equal(t, "linux", req.Metadata.Platform)
	require.Equal(t, "extension", req.Metadata.PluginType)
}

func TestOnboardUserResponse_Fields(t *testing.T) {
	resp := OnboardUserResponse{
		Done: true,
		Response: &OnboardUserResultData{
			CloudAICompanionProject: "project123",
		},
		Name: "test",
	}
	require.True(t, resp.Done)
	require.Equal(t, "test", resp.Name)
	require.NotNil(t, resp.Response)
}

func TestLoadCodeAssistRequest_Fields(t *testing.T) {
	req := LoadCodeAssistRequest{
		Metadata: LoadCodeAssistMetadata{
			IDEType:    "vscode",
			Platform:   "macos",
			PluginType: "extension",
		},
	}
	require.Equal(t, "vscode", req.Metadata.IDEType)
	require.Equal(t, "macos", req.Metadata.Platform)
	require.Equal(t, "extension", req.Metadata.PluginType)
}
