// backend/internal/jellyfin.go
package internal

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"time"
)

// AuthResult is the subset of Jellyfin auth response Pegasus needs.
type AuthResult struct {
	AccessToken string `json:"AccessToken"`
	User        struct {
		Id   string `json:"Id"`
		Name string `json:"Name"`
	} `json:"User"`
}

// Jellyfin is a tiny client for the auth and refresh endpoints Pegasus uses.
type Jellyfin struct {
	BaseURL string
	Client  *http.Client
}

// NewJellyfin builds a lightweight client for Jellyfin auth and refresh calls.
func NewJellyfin() *Jellyfin {
	return &Jellyfin{
		BaseURL: os.Getenv("JELLYFIN_URL"),
		Client:  &http.Client{Timeout: 10 * time.Second},
	}
}

// AuthenticateByName signs into Jellyfin with the plaintext password Jellyfin expects.
func (j *Jellyfin) AuthenticateByName(username, password string) (AuthResult, error) {
	var out AuthResult
	body := map[string]string{"Username": username, "Pw": password}
	b, _ := json.Marshal(body)

	req, _ := http.NewRequest("POST", j.BaseURL+"/Users/AuthenticateByName", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	// Jellyfin client descriptor (no token yet)
	req.Header.Set("Authorization",
		`MediaBrowser Client="Pegasus", Device="Pegasus Web", DeviceId="pegasus-web", Version="1.0.0"`)

	resp, err := j.Client.Do(req)
	if err != nil {
		return out, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return out, fmt.Errorf("login failed: %s", resp.Status)
	}
	if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
		return out, err
	}
	return out, nil
}
