package tails

import (
	"fmt"
	"log"

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

type Tails struct {
	NameSubstr string
	Relver     string
	URL        string
}

func (t Tails) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
	// Set torrentURLs for Tails mirror
	t.URL = "https://tails.net/torrents/files/"
	torrentURLs := []string{
		fmt.Sprintf("%s/tails-amd64-%s.img.torrent", t.URL, t.Relver),
		fmt.Sprintf("%s/tails-amd64-%s.iso.torrent", t.URL, t.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 (t Tails) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
	if err := common.RemoveTorrents(t.NameSubstr, t.Relver, transmissionbt); err != nil {
		return err
	}

	return nil
}
