package oci

import (
	"testing"
	"time"

	"github.com/oracle/oci-go-sdk/v65/common"
	"github.com/oracle/oci-go-sdk/v65/identity"
	"github.com/oracle/oci-go-sdk/v65/identitydomains"
)

func domainSummary(id, name, url string, state identity.DomainLifecycleStateEnum) identity.DomainSummary {
	return identity.DomainSummary{Id: &id, DisplayName: &name, Url: &url, LifecycleState: state}
}

func TestPickDomainURL(t *testing.T) {
	domains := []identity.DomainSummary{
		domainSummary("ocid1.domain.idcs", "OracleIdentityCloudService", "https://idcs-a.example.com", identity.DomainLifecycleStateActive),
		domainSummary("ocid1.domain.default", "Default", "https://idcs-b.example.com", identity.DomainLifecycleStateActive),
		domainSummary("ocid1.domain.off", "Off", "https://idcs-c.example.com", identity.DomainLifecycleStateInactive),
	}
	cases := []struct {
		name     string
		domains  []identity.DomainSummary
		domainID string
		want     string
	}{
		{"缺省优先 Default 域", domains, "", "https://idcs-b.example.com"},
		{"按 OCID 精确匹配", domains, "ocid1.domain.idcs", "https://idcs-a.example.com"},
		{"OCID 不存在返回空", domains, "ocid1.domain.miss", ""},
		{"非 ACTIVE 域不可选", domains, "ocid1.domain.off", ""},
		{"无 Default 时取第一个 ACTIVE", domains[:1], "", "https://idcs-a.example.com"},
		{"空列表返回空", nil, "", ""},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if got := pickDomainURL(tc.domains, tc.domainID); got != tc.want {
				t.Fatalf("pickDomainURL() = %q, want %q", got, tc.want)
			}
		})
	}
}

func TestToDomainListUser(t *testing.T) {
	created := "2024-12-12T14:52:00Z"
	lastLogin := "2026-07-01T08:00:00.123Z"
	yes, no := true, false
	u := identitydomains.User{
		Id:       common.String("scim-1"),
		Ocid:     common.String("ocid1.user.oc1..a"),
		UserName: common.String("oci"),
		Active:   &yes,
		Meta:     &identitydomains.Meta{Created: &created},
		Emails: []identitydomains.UserEmails{{
			Value: common.String("a@b.c"), Primary: &yes, Verified: &yes,
			Type: identitydomains.UserEmailsTypeWork,
		}},
		UrnIetfParamsScimSchemasOracleIdcsExtensionMfaUser: &identitydomains.ExtensionMfaUser{
			MfaStatus: identitydomains.ExtensionMfaUserMfaStatusEnrolled,
		},
		UrnIetfParamsScimSchemasOracleIdcsExtensionUserStateUser: &identitydomains.ExtensionUserStateUser{
			LastSuccessfulLoginDate: &lastLogin,
		},
	}
	got := toDomainListUser(u, "ocid1.user.oc1..a")
	if got.ID != "ocid1.user.oc1..a" || !got.IsCurrentUser {
		t.Fatalf("ID/IsCurrentUser 映射错误: %+v", got)
	}
	if !got.MfaActivated || !got.EmailVerified || got.Email != "a@b.c" {
		t.Fatalf("MFA/邮箱映射错误: %+v", got)
	}
	if got.LifecycleState != "ACTIVE" || got.TimeCreated == nil || got.LastLoginTime == nil {
		t.Fatalf("状态/时间映射错误: %+v", got)
	}

	inactive := identitydomains.User{Id: common.String("scim-2"), Ocid: common.String("ocid1.user.oc1..b"), Active: &no}
	if s := toDomainListUser(inactive, "x").LifecycleState; s != "INACTIVE" {
		t.Fatalf("inactive 用户 LifecycleState = %q, want INACTIVE", s)
	}
}

func TestParseScimTime(t *testing.T) {
	valid := "2026-07-10T01:02:03Z"
	bad := "not-a-time"
	empty := ""
	cases := []struct {
		name string
		in   *string
		want *time.Time
	}{
		{"合法 RFC3339", &valid, func() *time.Time { t0, _ := time.Parse(time.RFC3339, valid); return &t0 }()},
		{"nil 返回 nil", nil, nil},
		{"空串返回 nil", &empty, nil},
		{"非法格式返回 nil", &bad, nil},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got := parseScimTime(tc.in)
			if (got == nil) != (tc.want == nil) {
				t.Fatalf("parseScimTime() = %v, want %v", got, tc.want)
			}
			if got != nil && !got.Equal(*tc.want) {
				t.Fatalf("parseScimTime() = %v, want %v", got, tc.want)
			}
		})
	}
}
