package almalinux

import (
	"fmt"
	"log"

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

type AlmaLinux struct {
	NameSubstr string
	Relver     string
	URL        string
}

func (a AlmaLinux) AddNewTorrents(transmissionbt *transmissionrpc.Client) error {
	// Set torrentURLs for AlmaLinux mirror
	a.URL = "https://almalinux-mirror.dal1.hivelocity.net/"
	torrentURLs := []string{
		fmt.Sprintf("%s/%s/isos/aarch64/AlmaLinux-%s-aarch64.torrent", a.URL, a.Relver, a.Relver),
		fmt.Sprintf("%s/%s/isos/ppc64le/AlmaLinux-%s-ppc64le.torrent", a.URL, a.Relver, a.Relver),
		fmt.Sprintf("%s/%s/isos/s390x/AlmaLinux-%s-s390x.torrent", a.URL, a.Relver, a.Relver),
		fmt.Sprintf("%s/%s/isos/x86_64/AlmaLinux-%s-x86_64.torrent", a.URL, a.Relver, a.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 (a AlmaLinux) RemoveOldTorrents(transmissionbt *transmissionrpc.Client) error {
	if err := common.RemoveTorrents(a.NameSubstr, a.Relver, transmissionbt); err != nil {
		return err
	}

	return nil
}
