package app

import (
	"context"

	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
)

type DataCleanupService struct {
	serverSvc *ServerService
	pub       DataCleanupPublisher
}

func NewDataCleanupService(serverSvc *ServerService, pub DataCleanupPublisher) *DataCleanupService {
	return &DataCleanupService{serverSvc: serverSvc, pub: pub}
}

func (svc *DataCleanupService) CleanUp(ctx context.Context) error {
	params := domain.NewListServersParams()
	if err := params.SetOpen(domain.NullBool{
		V:     false,
		Valid: true,
	}); err != nil {
		return err
	}
	if err := params.SetSpecial(domain.NullBool{
		V:     false,
		Valid: true,
	}); err != nil {
		return err
	}

	servers, err := svc.serverSvc.ListAll(ctx, params)
	if err != nil {
		return err
	}

	payloads, err := servers.CleanUpData()
	if err != nil {
		return err
	}

	return svc.pub.CmdCleanUp(ctx, payloads...)
}
