package service

import (
	"context"
	"encoding/json"
	"fmt"
	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
	"github.com/go-nunu/nunu-layout-advanced/internal/model"
	"github.com/go-nunu/nunu-layout-advanced/internal/repository"
)

type GateWayGroupIpService interface {
	GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error)
	GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error)
	AddGateWayGroupIp(ctx context.Context,  req *v1.GateWayGroupIpRequest) error
	EditGateWayGroupIp(ctx context.Context,  req *v1.GateWayGroupIpRequest) error
	DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error
	GetGateWayGroupIpAdmin(ctx context.Context,req *v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error)

	//hostid获取网关组ip']\\
	GetGateWayGroupIpByHostId(ctx context.Context, hostId int) ([]string, error)
}

func NewGateWayGroupIpService(
	service *Service,
	gateWayGroupIpRepository repository.GateWayGroupIpRepository,
	gateWayGroupRep repository.GatewayGroupRepository,
	request RequestService,
) GateWayGroupIpService {
	return &gateWayGroupIpService{
		Service:                  service,
		gateWayGroupIpRepository: gateWayGroupIpRepository,
		request: request,
		gateWayGroupRep: gateWayGroupRep,
	}
}

type gateWayGroupIpService struct {
	*Service
	gateWayGroupIpRepository repository.GateWayGroupIpRepository
	request RequestService
	gateWayGroupRep repository.GatewayGroupRepository
}

func (s *gateWayGroupIpService) GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error) {
	res, err := s.gateWayGroupIpRepository.GetGateWayGroupIp(ctx, id)
	if err != nil {
		return nil, err
	}
	return res, nil
}

func (s *gateWayGroupIpService) GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error) {
	res, err := s.gateWayGroupIpRepository.GetGateWayGroupIpByGatewayGroupId(ctx, gatewayGroupId)
	if err != nil {
		return nil, err
	}
	return &res, nil
}

func (s *gateWayGroupIpService) AddGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
	if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
		return err
	}
	if err := s.gateWayGroupIpRepository.AddGateWayGroupIp(ctx, &model.GateWayGroupIp{
		GatewayGroupId: req.GatewayGroupId,
		Ip:             req.Ip,
		Tag:            req.Tag,
		Comment:        req.Comment,
		OriginPlace:    req.OriginPlace,
	}); err != nil {
		return err
	}
	return nil
}

func (s *gateWayGroupIpService) EditGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
	oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
	if err != nil {
		return err
	}
	if oldIp.Ip != req.Ip {
		if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
			return err
		}
		if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
			return err
		}
	}
	if err := s.gateWayGroupIpRepository.EditGateWayGroupIp(ctx, &model.GateWayGroupIp{
		Id:             req.Id,
		GatewayGroupId: req.GatewayGroupId,
		Ip:             req.Ip,
		Tag:            req.Tag,
		Comment:        req.Comment,
		OriginPlace:    req.OriginPlace,
	}); err != nil {
		return err
	}
	return nil
}

func (s *gateWayGroupIpService) DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error {
	oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
	if err != nil {
		return err
	}
	if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
		return err
	}
	if err := s.gateWayGroupIpRepository.DeleteGateWayGroupIp(ctx, &model.GateWayGroupIp{
		Id: req.Id,
	}); err != nil {
		return err
	}
	return nil
}

func (s *gateWayGroupIpService) GetGateWayGroupIpAdmin(ctx context.Context,req *v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error) {
	res, err := s.gateWayGroupIpRepository.GetGatewayGroupIpList(ctx, *req)
	if err != nil {
		return nil, err
	}
	return res, nil

}

func (s *gateWayGroupIpService) sendIp(ctx context.Context, ip string, action string) error  {
	var apiUrl string
	switch action {
	case "add":
		apiUrl = ""
	case "delete":

		apiUrl = ""
	}
	formData := map[string]interface{}{
		"ip": ip,
	}
	resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
	if err != nil {
		return err
	}
	var res v1.GeneralResponse[string]
	err = json.Unmarshal(resBody, &res)
	if err != nil {
		return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
	}
	if res.Code != 0 {
		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
	}
	return nil

}

func (s *gateWayGroupIpService) GetGateWayGroupIpByHostId(ctx context.Context, hostId int) ([]string, error) {
	gatewayGroup, err := s.gateWayGroupRep.GetGatewayGroupByHostId(ctx, int64(hostId))
	if err != nil {
		return nil, err
	}
	res, err := s.gateWayGroupIpRepository.GetGateWayGroupAllIpByGatewayGroupId(ctx, gatewayGroup.Id)
	if err != nil {
		return nil, err
	}
	return res, nil
}