package qbit

import "encoding/json"

// Torrent mirrors the JSON shape qBittorrent's WebUI API returns from
// /api/v2/torrents/info and as values in /api/v2/sync/maindata.torrents.
// Only fields the Command Center reads are typed; unknown fields are
// retained in Extra for forward-compatibility.
type Torrent struct {
	Hash             string  `json:"hash"`
	Name             string  `json:"name"`
	Size             int64   `json:"size"`
	Progress         float64 `json:"progress"`
	State            string  `json:"state"`
	Category         string  `json:"category"`
	Tags             string  `json:"tags"`
	Uploaded         int64   `json:"uploaded"`
	Downloaded       int64   `json:"downloaded"`
	Ratio            float64 `json:"ratio"`
	AddedOn          int64   `json:"added_on"`
	CompletionOn     int64   `json:"completion_on"`
	SeedingTime      int64   `json:"seeding_time"`
	UpSpeed          int64   `json:"upspeed"`
	DlSpeed          int64   `json:"dlspeed"`
	NumSeeds         int     `json:"num_seeds"`
	NumLeechs        int     `json:"num_leechs"`
	NumComplete      int     `json:"num_complete"`
	NumIncomplete    int     `json:"num_incomplete"`
	Tracker          string  `json:"tracker"`
	MagnetURI        string  `json:"magnet_uri"`
	SavePath         string  `json:"save_path"`
	ContentPath      string  `json:"content_path"`

	// Extra captures any field the operator's qBittorrent version returns
	// that we haven't typed. Surfaced in the detail view as a raw object.
	Extra map[string]json.RawMessage `json:"-"`
}

// Tracker is the per-torrent tracker info returned by /api/v2/torrents/trackers.
type Tracker struct {
	URL       string `json:"url"`
	Status    int    `json:"status"`
	Tier      int    `json:"tier"`
	NumPeers  int    `json:"num_peers"`
	NumSeeds  int    `json:"num_seeds"`
	NumLeechs int    `json:"num_leeches"`
	Msg       string `json:"msg"`
}

// MainData is /api/v2/sync/maindata's response.
type MainData struct {
	RID             int                `json:"rid"`
	FullUpdate      bool               `json:"full_update"`
	Torrents        map[string]Torrent `json:"torrents"`
	TorrentsRemoved []string           `json:"torrents_removed"`
	Categories      map[string]any     `json:"categories"`
	Tags            []string           `json:"tags"`
	ServerState     map[string]any     `json:"server_state"`
}

// AddRequest is the typed shape for POST /api/torrents/add.
type AddRequest struct {
	// Magnet, if non-empty, takes precedence.
	Magnet string

	// TorrentFile is the .torrent bytes; mutually exclusive with Magnet.
	TorrentFile []byte

	// Optional add-time settings — all empty fields are omitted.
	Category    string
	Tags        []string
	SavePath    string
	Paused      bool   // start paused?
	RootFolder  string // "true"/"false"/""
	Rename      string
}
