package waf

import (
	"context"
	"fmt"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	adminApi "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
	"github.com/go-nunu/nunu-layout-advanced/internal/model"
	"github.com/go-nunu/nunu-layout-advanced/internal/repository/api/waf"
	"github.com/go-nunu/nunu-layout-advanced/internal/service"
	"github.com/go-nunu/nunu-layout-advanced/internal/service/admin"
)

type GatewayipService interface {
	GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
	GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error)
	GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error)
	AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error
}
func NewGatewayipService(
    service *service.Service,
    gatewayipRepository waf.GatewayipRepository,
	host service.HostService,
	log service.LogService,
	wafLogService admin.WafLogService,
) GatewayipService {
	return &gatewayipService{
		Service:        service,
		gatewayipRepository: gatewayipRepository,
		host : host,
		log : log,
		wafLogService : wafLogService,
	}
}

type gatewayipService struct {
	*service.Service
	gatewayipRepository waf.GatewayipRepository
	host                service.HostService
	log                 service.LogService
	wafLogService       admin.WafLogService
}

func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
	return s.gatewayipRepository.GetGatewayip(ctx, id)
}

func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
	config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
	if err != nil {
		return  err
	}

	 ips, err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
		HostId:              int(hostId),
		Bps:                 config.Bps,
		MaxBytesMonth:       config.MaxBytesMonth,
		IpCount:             config.IpCount,
		Operator:            config.Operator,
		NodeArea:            config.NodeArea,
		ConfigMaxProtection: config.ConfigMaxProtection,
		IsBanUdp:            config.IsBanUdp,
		IsBanOverseas:       config.IsBanOverseas,
	});
	 if err != nil {
		return  err
	}


	go s.wafLogService.PublishIpWafLogTask(ctx,adminApi.WafLog{
		Uid:       int(uid),
		Api:       "分配网关组", // 复制 Path
		HostId: int(hostId),
		ExtraData: ips,
	})

	return nil
}

func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
	gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
	if err != nil {
		return nil, err
	}

	if len(gatewayIps) == 0 {
		expire, err := s.host.CheckExpired(ctx, uid, hostId)
		if err != nil {
			return nil, err
		}
		if !expire {
			return nil, fmt.Errorf("产品已过期,请及时续费")
		}

		err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
		if err != nil {
			return nil, err
		}
		gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
		if err != nil {
			return nil, err
		}
	}

	return gatewayIps, nil
}


func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
	gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
	if err != nil {
		return "", err
	}

	if len(gatewayIps) == 0 {
		err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
		if err != nil {
			return "", err
		}
		gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
		if err != nil {
			return "", err
		}
	}

	return gatewayIps, nil
}