package integration

import (
	"encoding/json"
	"fmt"
	"testing"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/response"
	"github.com/break/junhong_cmp_fiber/tests/testutils/integ"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// TestGatewayCard_GetStatus 测试查询卡状态接口
func TestGatewayCard_GetStatus(t *testing.T) {
	env := integ.NewIntegrationTestEnv(t)

	card := &model.IotCard{
		ICCID:     "89860001234567890001",
		CardType:  "data_card",
		CarrierID: 1,
		Status:    1,
	}
	require.NoError(t, env.TX.Create(card).Error)

	t.Run("成功查询卡状态", func(t *testing.T) {
		resp, err := env.AsSuperAdmin().Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/gateway-status", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 200, resp.StatusCode)

		var result response.Response
		err = json.NewDecoder(resp.Body).Decode(&result)
		require.NoError(t, err)
		assert.Equal(t, 0, result.Code)
	})

	t.Run("无权限访问其他店铺的卡", func(t *testing.T) {
		shop2 := &model.Shop{
			ShopName: "测试店铺2",
			ShopCode: "SHOP_002",
			Level:    1,
		}
		require.NoError(t, env.TX.Create(shop2).Error)

		card2 := &model.IotCard{
			ICCID:     "89860001234567890002",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
			ShopID:    &shop2.ID,
		}
		require.NoError(t, env.TX.Create(card2).Error)

		agentAccount := &model.Account{
			Username: "agent_test_gateway_1",
			Phone:    "13800000101",
			UserType: constants.UserTypeAgent,
			ShopID:   &shop2.ID,
			Status:   1,
		}
		require.NoError(t, env.TX.Create(agentAccount).Error)

		resp, err := env.AsUser(agentAccount).Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/gateway-status", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 404, resp.StatusCode)
	})
}

// TestGatewayCard_GetFlow 测试查询流量接口
func TestGatewayCard_GetFlow(t *testing.T) {
	env := integ.NewIntegrationTestEnv(t)

	card := &model.IotCard{
		ICCID:     "89860001234567890003",
		CardType:  "data_card",
		CarrierID: 1,
		Status:    1,
	}
	require.NoError(t, env.TX.Create(card).Error)

	t.Run("成功查询流量使用", func(t *testing.T) {
		resp, err := env.AsSuperAdmin().Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/gateway-flow", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 200, resp.StatusCode)

		var result response.Response
		err = json.NewDecoder(resp.Body).Decode(&result)
		require.NoError(t, err)
		assert.Equal(t, 0, result.Code)
	})

	t.Run("无权限访问其他店铺的卡流量", func(t *testing.T) {
		shop2 := &model.Shop{
			ShopName: "测试店铺3",
			ShopCode: "SHOP_003",
			Level:    1,
		}
		require.NoError(t, env.TX.Create(shop2).Error)

		card2 := &model.IotCard{
			ICCID:     "89860001234567890004",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
			ShopID:    &shop2.ID,
		}
		require.NoError(t, env.TX.Create(card2).Error)

		agentAccount := &model.Account{
			Username: "agent_test_gateway_2",
			Phone:    "13800000102",
			UserType: constants.UserTypeAgent,
			ShopID:   &shop2.ID,
			Status:   1,
		}
		require.NoError(t, env.TX.Create(agentAccount).Error)

		resp, err := env.AsUser(agentAccount).Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/gateway-flow", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 404, resp.StatusCode)
	})
}

// TestGatewayCard_GetRealname 测试查询实名状态接口
func TestGatewayCard_GetRealname(t *testing.T) {
	env := integ.NewIntegrationTestEnv(t)

	card := &model.IotCard{
		ICCID:     "89860001234567890005",
		CardType:  "data_card",
		CarrierID: 1,
		Status:    1,
	}
	require.NoError(t, env.TX.Create(card).Error)

	t.Run("成功查询实名状态", func(t *testing.T) {
		resp, err := env.AsSuperAdmin().Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/gateway-realname", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 200, resp.StatusCode)

		var result response.Response
		err = json.NewDecoder(resp.Body).Decode(&result)
		require.NoError(t, err)
		assert.Equal(t, 0, result.Code)
	})

	t.Run("无权限访问其他店铺的卡实名状态", func(t *testing.T) {
		shop2 := &model.Shop{
			ShopName: "测试店铺4",
			ShopCode: "SHOP_004",
			Level:    1,
		}
		require.NoError(t, env.TX.Create(shop2).Error)

		card2 := &model.IotCard{
			ICCID:     "89860001234567890006",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
			ShopID:    &shop2.ID,
		}
		require.NoError(t, env.TX.Create(card2).Error)

		agentAccount := &model.Account{
			Username: "agent_test_gateway_3",
			Phone:    "13800000103",
			UserType: constants.UserTypeAgent,
			ShopID:   &shop2.ID,
			Status:   1,
		}
		require.NoError(t, env.TX.Create(agentAccount).Error)

		resp, err := env.AsUser(agentAccount).Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/gateway-realname", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 404, resp.StatusCode)
	})
}

// TestGatewayCard_GetRealnameLink 测试获取实名链接接口
func TestGatewayCard_GetRealnameLink(t *testing.T) {
	env := integ.NewIntegrationTestEnv(t)

	card := &model.IotCard{
		ICCID:     "89860001234567890007",
		CardType:  "data_card",
		CarrierID: 1,
		Status:    1,
	}
	require.NoError(t, env.TX.Create(card).Error)

	t.Run("成功获取实名链接", func(t *testing.T) {
		resp, err := env.AsSuperAdmin().Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/realname-link", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 200, resp.StatusCode)

		var result response.Response
		err = json.NewDecoder(resp.Body).Decode(&result)
		require.NoError(t, err)
		assert.Equal(t, 0, result.Code)
	})

	t.Run("无权限访问其他店铺的卡实名链接", func(t *testing.T) {
		shop2 := &model.Shop{
			ShopName: "测试店铺5",
			ShopCode: "SHOP_005",
			Level:    1,
		}
		require.NoError(t, env.TX.Create(shop2).Error)

		card2 := &model.IotCard{
			ICCID:     "89860001234567890008",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
			ShopID:    &shop2.ID,
		}
		require.NoError(t, env.TX.Create(card2).Error)

		agentAccount := &model.Account{
			Username: "agent_test_gateway_4",
			Phone:    "13800000104",
			UserType: constants.UserTypeAgent,
			ShopID:   &shop2.ID,
			Status:   1,
		}
		require.NoError(t, env.TX.Create(agentAccount).Error)

		resp, err := env.AsUser(agentAccount).Request("GET", fmt.Sprintf("/api/admin/iot-cards/%s/realname-link", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 404, resp.StatusCode)
	})
}

// TestGatewayCard_StopCard 测试停机接口
func TestGatewayCard_StopCard(t *testing.T) {
	env := integ.NewIntegrationTestEnv(t)

	card := &model.IotCard{
		ICCID:     "89860001234567890009",
		CardType:  "data_card",
		CarrierID: 1,
		Status:    1,
	}
	require.NoError(t, env.TX.Create(card).Error)

	t.Run("成功停机", func(t *testing.T) {
		resp, err := env.AsSuperAdmin().Request("POST", fmt.Sprintf("/api/admin/iot-cards/%s/stop", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 200, resp.StatusCode)

		var result response.Response
		err = json.NewDecoder(resp.Body).Decode(&result)
		require.NoError(t, err)
		assert.Equal(t, 0, result.Code)
	})

	t.Run("无权限停机其他店铺的卡", func(t *testing.T) {
		shop2 := &model.Shop{
			ShopName: "测试店铺6",
			ShopCode: "SHOP_006",
			Level:    1,
		}
		require.NoError(t, env.TX.Create(shop2).Error)

		card2 := &model.IotCard{
			ICCID:     "89860001234567890010",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
			ShopID:    &shop2.ID,
		}
		require.NoError(t, env.TX.Create(card2).Error)

		agentAccount := &model.Account{
			Username: "agent_test_gateway_5",
			Phone:    "13800000105",
			UserType: constants.UserTypeAgent,
			ShopID:   &shop2.ID,
			Status:   1,
		}
		require.NoError(t, env.TX.Create(agentAccount).Error)

		resp, err := env.AsUser(agentAccount).Request("POST", fmt.Sprintf("/api/admin/iot-cards/%s/stop", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 404, resp.StatusCode)
	})
}

// TestGatewayCard_StartCard 测试复机接口
func TestGatewayCard_StartCard(t *testing.T) {
	env := integ.NewIntegrationTestEnv(t)

	card := &model.IotCard{
		ICCID:     "89860001234567890011",
		CardType:  "data_card",
		CarrierID: 1,
		Status:    1,
	}
	require.NoError(t, env.TX.Create(card).Error)

	t.Run("成功复机", func(t *testing.T) {
		resp, err := env.AsSuperAdmin().Request("POST", fmt.Sprintf("/api/admin/iot-cards/%s/start", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 200, resp.StatusCode)

		var result response.Response
		err = json.NewDecoder(resp.Body).Decode(&result)
		require.NoError(t, err)
		assert.Equal(t, 0, result.Code)
	})

	t.Run("无权限复机其他店铺的卡", func(t *testing.T) {
		shop2 := &model.Shop{
			ShopName: "测试店铺7",
			ShopCode: "SHOP_007",
			Level:    1,
		}
		require.NoError(t, env.TX.Create(shop2).Error)

		card2 := &model.IotCard{
			ICCID:     "89860001234567890012",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
			ShopID:    &shop2.ID,
		}
		require.NoError(t, env.TX.Create(card2).Error)

		agentAccount := &model.Account{
			Username: "agent_test_gateway_6",
			Phone:    "13800000106",
			UserType: constants.UserTypeAgent,
			ShopID:   &shop2.ID,
			Status:   1,
		}
		require.NoError(t, env.TX.Create(agentAccount).Error)

		resp, err := env.AsUser(agentAccount).Request("POST", fmt.Sprintf("/api/admin/iot-cards/%s/start", card.ICCID), nil)
		require.NoError(t, err)
		defer resp.Body.Close()

		assert.Equal(t, 404, resp.StatusCode)
	})
}
