package service

import (
	"context"
	"testing"

	"oci-portal/internal/oci"
)

func TestSubscriptionsPassThrough(t *testing.T) {
	client := &fakeClient{
		tenancy: oci.TenancyInfo{Name: "t", HomeRegionKey: "FRA"},
		subscriptions: []oci.SubscriptionInfo{
			{ID: "ocid1.organizationssubscription.oc1..a", ServiceName: "CLOUDCM"},
			{ID: "ocid1.organizationssubscription.oc1..b", ServiceName: "SAAS"},
		},
	}
	svc := newTestService(t, client)
	cfg := importAliveConfig(t, svc)

	subs, err := svc.Subscriptions(context.Background(), cfg.ID)
	if err != nil {
		t.Fatalf("Subscriptions: %v", err)
	}
	if len(subs) != 2 {
		t.Fatalf("len(subs) = %d, want 2", len(subs))
	}
	if got, want := subs[0].ServiceName, "CLOUDCM"; got != want {
		t.Errorf("subs[0].ServiceName = %q, want %q", got, want)
	}
}

func TestSubscriptionDetailPassesID(t *testing.T) {
	client := &fakeClient{
		tenancy: oci.TenancyInfo{Name: "t", HomeRegionKey: "FRA"},
		subDetail: oci.SubscriptionDetail{
			ID:           "ocid1.organizationssubscription.oc1..a",
			ServiceName:  "CLOUDCM",
			PaymentModel: "FREE_TRIAL",
			Promotions:   []oci.PromotionInfo{{Status: "ACTIVE", Amount: 300}},
		},
	}
	svc := newTestService(t, client)
	cfg := importAliveConfig(t, svc)

	detail, err := svc.SubscriptionDetail(context.Background(), cfg.ID, "ocid1.organizationssubscription.oc1..a")
	if err != nil {
		t.Fatalf("SubscriptionDetail: %v", err)
	}
	if got, want := client.subDetailID, "ocid1.organizationssubscription.oc1..a"; got != want {
		t.Errorf("passed subscription id = %q, want %q", got, want)
	}
	if len(detail.Promotions) != 1 || detail.Promotions[0].Amount != 300 {
		t.Errorf("Promotions = %+v, want one ACTIVE amount 300", detail.Promotions)
	}
}

func TestSubscriptionDetailRejectsEmptyID(t *testing.T) {
	svc := newTestService(t, &fakeClient{tenancy: oci.TenancyInfo{Name: "t"}})
	cfg := importAliveConfig(t, svc)
	if _, err := svc.SubscriptionDetail(context.Background(), cfg.ID, ""); err == nil {
		t.Error("SubscriptionDetail(empty id): got nil error, want failure")
	}
}

func TestLimitServicesPassThrough(t *testing.T) {
	client := &fakeClient{
		tenancy: oci.TenancyInfo{Name: "t", HomeRegionKey: "FRA"},
		limitServices: []oci.LimitService{
			{Name: "compute", Description: "Compute"},
			{Name: "vcn", Description: "Virtual Cloud Network"},
		},
	}
	svc := newTestService(t, client)
	cfg := importAliveConfig(t, svc)

	services, err := svc.LimitServices(context.Background(), cfg.ID, "")
	if err != nil {
		t.Fatalf("LimitServices: %v", err)
	}
	if len(services) != 2 {
		t.Fatalf("len(services) = %d, want 2", len(services))
	}
	if got, want := services[0].Name, "compute"; got != want {
		t.Errorf("services[0].Name = %q, want %q", got, want)
	}
}
