package waf

import (
	"context"
	"fmt"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	flexCdnRep "github.com/go-nunu/nunu-layout-advanced/internal/repository/api/flexCdn"
	"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/api/flexCdn"
	"time"
)

type CcService interface {
	GetCcList(ctx context.Context, req v1.CCListRequest) ([]v1.CCListResponse, error)
	EditCcState(ctx context.Context, req v1.CCStateRequest) error
	// 修改CC配置
	EditCcConfig(ctx context.Context,webId int64, req v1.CcConfigRequest) error
	// 一键加白
	AddWhiteOneClick (ctx context.Context, req v1.CCStateRequest) error
}
func NewCcService(
    service *service.Service,
    ccRepository flexCdnRep.CcRepository,
	webForwardingRep waf.WebForwardingRepository,
	cdn flexCdn.CdnService,
	ccIpList CcIpListService,
) CcService {
	return &ccService{
		Service:        service,
		ccRepository: ccRepository,
		webForwardingRep: webForwardingRep,
		cdn: cdn,
		ccIpList: ccIpList,
	}
}

type ccService struct {
	*service.Service
	ccRepository     flexCdnRep.CcRepository
	webForwardingRep waf.WebForwardingRepository
	cdn              flexCdn.CdnService
	ccIpList         CcIpListService
}

func (s *ccService) GetCcList(ctx context.Context, req v1.CCListRequest) ([]v1.CCListResponse, error) {
	webData, err := s.webForwardingRep.GetWebForwarding(ctx, req.WebId)
	if err != nil {
		return nil, err
	}
	if webData.CdnWebId == 0 {
		return nil, fmt.Errorf("网站不存在")
	}
	ccList, err := s.ccRepository.GetCcList(ctx, int64(webData.CdnWebId))
	if err != nil {
		return nil, err
	}
	var res []v1.CCListResponse
	for _, v := range ccList {
		res = append(res, v1.CCListResponse{
			Value: v.Value,
			Type: v.Type,
			Reason: v.Reason,
			SourceURL: v.SourceURL,
			SourceUserAgent: v.SourceUserAgent,
			CreatedAt: time.Unix(v.CreatedAt, 0).Format("2006-01-02 15:04:05"),
			ExpiredAt: time.Unix(v.ExpiredAt, 0).Format("2006-01-02 15:04:05"),
		})
	}
	return res, nil
}

func (s *ccService) EditCcState(ctx context.Context, req v1.CCStateRequest) error {
	for _, v := range req.Ips {
		webData, err := s.webForwardingRep.GetWebForwarding(ctx, req.WebId)
		if err != nil {
			return err
		}
		if webData.CdnWebId == 0 {
			return fmt.Errorf("网站不存在")
		}
		ccId, err := s.ccRepository.GetCcId(ctx, int64(webData.CdnWebId), v)
		if err != nil {
			return err
		}
		if ccId == 0 {
			return fmt.Errorf("IP不存在")
		}
		err = s.cdn.DelIpItem(ctx, ccId, v, "", "", 2000000000)
		if err != nil {
			return err
		}

	}
	return nil
}

// 一键加白
func (s *ccService) AddWhiteOneClick (ctx context.Context, req v1.CCStateRequest) error {
	err := s.EditCcState(ctx, req)
	if err != nil {
		return err
	}

	for _, v := range req.Ips {
		err = s.ccIpList.AddCcWhiteIp(ctx, req.WebId, v, "")
		if err != nil {
			return err
		}
	}
	return nil
}


// 修改CC配置
func (s *ccService) EditCcConfig(ctx context.Context,webId int64, req v1.CcConfigRequest) error {
	webConfigId, err := s.webForwardingRep.GetWebConfigId(ctx, webId)
	if err != nil {
		return err
	}


	configThreshold := []struct{
		MaxRequests   int `json:"maxRequests"`
		BlockSeconds  int `json:"blockSeconds"`
		PeriodSeconds int `json:"periodSeconds"`
	}{
		{MaxRequests:  req.Limit5s},
		{MaxRequests:  req.Limit60s},
		{MaxRequests:  req.Limit300s},
	}

	if err := s.cdn.EditCcConfig(ctx, webConfigId, v1.CcConfig{
		IsOn: req.IsOn,
		ThresholdMethod: req.ThresholdMethod,
		Thresholds: configThreshold,
		Level: req.Level,
		UseDefaultThresholds : true,
		EnableGET302: true,
		EnableFingerprint : true,
		IgnoreCommonFiles : true,
	}); err != nil {
		return err
	}
	return nil
}