package oci

import (
	"testing"

	"oci-portal/internal/model"
)

func TestClassifyAccount(t *testing.T) {
	tests := []struct {
		name            string
		paymentModel    string
		tier            string
		promotionStatus string
		want            string
	}{
		{name: "按量付费为付费账户", paymentModel: "Pay as you go", want: model.AccountTypePaid},
		{name: "月度账单为付费账户", paymentModel: "Monthly", want: model.AccountTypePaid},
		{name: "试用中促销激活", paymentModel: "FREE_TRIAL", promotionStatus: "ACTIVE", want: model.AccountTypeTrial},
		{name: "试用中按层级判定", paymentModel: "FREE_TRIAL", tier: "FREE_AND_TRIAL", want: model.AccountTypeTrial},
		{name: "促销过期为免费账户", paymentModel: "FREE_TRIAL", promotionStatus: "EXPIRED", want: model.AccountTypeFree},
		{name: "免费层级为免费账户", paymentModel: "FREE_TRIAL", tier: "FREE", want: model.AccountTypeFree},
		{name: "促销激活优先于免费层级", paymentModel: "FREE_TRIAL", tier: "FREE", promotionStatus: "ACTIVE", want: model.AccountTypeTrial},
		{name: "试用但无促销无层级", paymentModel: "FREE_TRIAL", want: model.AccountTypeUnknown},
		{name: "全部为空无法判定", want: model.AccountTypeUnknown},
		{name: "大小写不敏感", paymentModel: "free_trial", promotionStatus: "active", want: model.AccountTypeTrial},
		{name: "促销初始化状态不判定", paymentModel: "FREE_TRIAL", promotionStatus: "INITIALIZED", want: model.AccountTypeUnknown},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := ClassifyAccount(tt.paymentModel, tt.tier, tt.promotionStatus)
			if got != tt.want {
				t.Errorf("ClassifyAccount(%q, %q, %q) = %q, want %q",
					tt.paymentModel, tt.tier, tt.promotionStatus, got, tt.want)
			}
		})
	}
}

// TestParseAccountCapabilities 用控制台同源接口的真实响应样例驱动。
func TestParseAccountCapabilities(t *testing.T) {
	cases := []struct {
		name       string
		body       string
		wantSusp   bool
		wantStatus string
		wantErr    bool
	}{
		{
			name: "已终止租户(顶层与 accountInfo 双 suspended)",
			body: `{"compartmentId":"ocid1.tenancy.oc1..x","capabilities":{"suspended":true,` +
				`"accountInfo":{"freeTierEnabled":false,"freeTierOnly":false,"promotionStatus":"none",` +
				`"suspended":true,"programType":"default","limitsProvisioned":true,"intentToPay":false,` +
				`"hasSaasSubscription":true,"accountStatus":"terminated","accountFlags":0,"deletable":true,` +
				`"limitIncrease":false,"orgProperties":"0"}}}`,
			wantSusp:   true,
			wantStatus: "terminated",
		},
		{
			name:     "正常租户",
			body:     `{"capabilities":{"suspended":false,"accountInfo":{"accountStatus":"active","freeTierEnabled":true}}}`,
			wantSusp: false, wantStatus: "active",
		},
		{
			name:     "仅 accountInfo 标记 suspended 也算暂停",
			body:     `{"capabilities":{"suspended":false,"accountInfo":{"suspended":true}}}`,
			wantSusp: true,
		},
		{name: "非 JSON 报错", body: `<html>`, wantErr: true},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseAccountCapabilities([]byte(tc.body))
			if tc.wantErr {
				if err == nil {
					t.Fatal("expected error")
				}
				return
			}
			if err != nil {
				t.Fatalf("parse: %v", err)
			}
			if got.Suspended != tc.wantSusp || got.AccountStatus != tc.wantStatus {
				t.Fatalf("got %+v, want suspended=%v status=%q", got, tc.wantSusp, tc.wantStatus)
			}
		})
	}
}
