package debian

import (
	"fmt"
	"log"

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

type Debian struct {
	NameSubstr string
	Relver     string
	URL        string
}

func (d Debian) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
	// Set torrentURLs for Debian GNU/Linux mirror
	d.URL = "https://cdimage.debian.org/debian-cd/current/"
	torrentURLs := []string{
		fmt.Sprintf("%s/amd64/bt-dvd/debian-%s-amd64-DVD-1.iso.torrent", d.URL, d.Relver),
		fmt.Sprintf("%s/arm64/bt-dvd/debian-%s-arm64-DVD-1.iso.torrent", d.URL, d.Relver),
		fmt.Sprintf("%s/ppc64el/bt-dvd/debian-%s-ppc64el-DVD-1.iso.torrent", d.URL, d.Relver),
		fmt.Sprintf("%s/s390x/bt-dvd/debian-%s-s390x-DVD-1.iso.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 Debian) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
	if err := common.RemoveTorrents(d.NameSubstr, d.Relver, transmissionbt); err != nil {
		return err
	}

	return nil
}
