package waf

import (
	"github.com/gin-gonic/gin"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	cdnStruct "github.com/go-nunu/nunu-layout-advanced/api/v1/cdn"
	"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"
)

type CdnLogService interface {
	GetAccessLog(ctx *gin.Context,req v1.GetCdnLog) (cdnStruct.Data, error)
}
func NewCdnLogService(
    service *service.Service,
	tcpforwardingRep waf.TcpforwardingRepository,
	udpforwardingRep waf.UdpForWardingRepository,
	webforwardingRep waf.WebForwardingRepository,
	cdn flexCdn.CdnService,
	ccIpList CcIpListService,
) CdnLogService {
	return &cdnLogService{
		Service:        service,
		tcpforwardingRep: tcpforwardingRep,
		udpforwardingRep: udpforwardingRep,
		webforwardingRep: webforwardingRep,
		cdn: cdn,
		ccIpList: ccIpList,
	}
}

type cdnLogService struct {
	*service.Service
	tcpforwardingRep waf.TcpforwardingRepository
	udpforwardingRep waf.UdpForWardingRepository
	webforwardingRep waf.WebForwardingRepository
	cdn flexCdn.CdnService
	ccIpList CcIpListService
}


func (s *cdnLogService) GetAccessLog(ctx *gin.Context,req v1.GetCdnLog) (cdnStruct.Data, error) {
	serverId, err := s.ccIpList.GetServerId(ctx,req.WebId)
	if err != nil {
		return cdnStruct.Data{}, err
	}

	logData, err := s.cdn.GetAccessLog(ctx,cdnStruct.Request{
		ServerID: serverId,
		Day: req.Day,
		Size: 50,
		Ip: req.Ip,
		Domain: req.Domain,
		HasError: req.HasError,
		HourFrom: req.HourFrom,
		HourTo: req.HourTo,
		Reverse: req.Reverse,
		RequestId: req.RequestId,
		Keyword: req.Keyword,
	})

	if err != nil {
		return cdnStruct.Data{}, err
	}
	return logData, nil

}