package servertype_test

import (
	"fmt"
	"testing"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/mock/gomock"

	"github.com/hetznercloud/cli/internal/cmd/servertype"
	"github.com/hetznercloud/cli/internal/cmd/util"
	"github.com/hetznercloud/cli/internal/testutil"
	"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestDescribe(t *testing.T) {
	fx := testutil.NewFixture(t)
	defer fx.Finish()

	cmd := servertype.DescribeCmd.CobraCommand(fx.State())
	fx.ExpectEnsureToken()

	deprecation := hcloud.DeprecatableResource{Deprecation: &hcloud.DeprecationInfo{
		Announced:        time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
		UnavailableAfter: time.Date(2025, 4, 1, 0, 0, 0, 0, time.UTC),
	}}

	fx.Client.ServerTypeClient.EXPECT().
		Get(gomock.Any(), "cax11").
		Return(&hcloud.ServerType{
			ID:          45,
			Name:        "cax11",
			Description: "CAX11",
			Category:    "Shared vCPU",
			Cores:       2,
			CPUType:     hcloud.CPUTypeShared,
			Memory:      4.0,
			Disk:        40,
			StorageType: hcloud.StorageTypeLocal,
			Locations: []hcloud.ServerTypeLocation{
				{
					Location:             &hcloud.Location{Name: "fsn1"},
					DeprecatableResource: deprecation,
					Available:            true,
					Recommended:          false,
				},
			},
		}, nil, nil)

	fx.Client.PricingClient.EXPECT().
		Get(gomock.Any()).
		Return(hcloud.Pricing{
			ServerTypes: []hcloud.ServerTypePricing{
				// Two server types to test that fullPricingInfo filters for the correct one
				{
					ServerType: &hcloud.ServerType{ID: 1},
					Pricings: []hcloud.ServerTypeLocationPricing{{
						Location: &hcloud.Location{
							Name: "nbg1",
						},
						Hourly: hcloud.Price{
							Gross:    "4.0000",
							Currency: "EUR",
						},
						Monthly: hcloud.Price{
							Gross:    "7.0000",
							Currency: "EUR",
						},
						IncludedTraffic: 6543210,
						PerTBTraffic: hcloud.Price{
							Gross:    "8.0000",
							Currency: "EUR",
						},
					}},
				},
				{
					ServerType: &hcloud.ServerType{ID: 45},
					Pricings: []hcloud.ServerTypeLocationPricing{{
						Location: &hcloud.Location{
							Name: "fsn1",
						},
						Hourly: hcloud.Price{
							Gross:    "1.0000",
							Currency: "EUR",
						},
						Monthly: hcloud.Price{
							Gross:    "2.0000",
							Currency: "EUR",
						},
						IncludedTraffic: 654321,
						PerTBTraffic: hcloud.Price{
							Gross:    "3.0000",
							Currency: "EUR",
						},
					}},
				},
			},
		}, nil, nil)

	out, errOut, err := fx.Run(cmd, []string{"cax11"})

	expOut := fmt.Sprintf(`ID:            45
Name:          cax11
Description:   CAX11
Category:      Shared vCPU
Cores:         2
CPU Type:      shared
Architecture:  
Memory:        4.0 GB
Disk:          40 GB
Storage Type:  local

Locations:
  - Location:     fsn1
    Available:    yes
    Recommended:  no
    Deprecation:
      Announced:           %s (%s)
      Unavailable After:   %s (%s)
    Pricing:               
      Hourly:              € 1.0000
      Monthly:             € 2.0000
      Included Traffic:    639 KiB
      Additional Traffic:  € 3.0000 per TB

`,
		util.Datetime(deprecation.DeprecationAnnounced()), humanize.Time(deprecation.DeprecationAnnounced()),
		util.Datetime(deprecation.UnavailableAfter()), humanize.Time(deprecation.UnavailableAfter()))

	require.NoError(t, err)
	assert.Empty(t, errOut)
	assert.Equal(t, expOut, out)
}
