package devuan

import (
	"fmt"
	"log"

	"codeberg.org/hyperreal/go-torrent-helper/common"
	"github.com/hekmon/transmissionrpc"
)

type Devuan struct {
	NameSubstr string
	Relver     string
	URL        string
}

func (d Devuan) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
	// Set torrentURLs for Devuan GNU+Linux mirror
	// https://files.devuan.org/devuan_daedalus.torrent
	d.URL = "https://files.devuan.org/devuan"
	torrentURLs := []string{
		fmt.Sprintf("%s_%s.torrent", d.URL, d.Relver),
	}

	// Add torrents to Transmission instance
	for _, torrentURL := range torrentURLs {
		torrent, err := transmissionbt.TorrentAdd(&transmissionrpc.TorrentAddPayload{
			Filename: &torrentURL,
		})
		if err != nil {
			return err
		}

		log.Printf("%s added\n", *torrent.Name)
	}

	return nil
}

func (d Devuan) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
	if err := common.RemoveTorrents(d.NameSubstr, d.Relver, transmissionbt); err != nil {
		return err
	}

	return nil
}
