package qobuz

import (
	"context"
	"os"
	"testing"
	"time"
)

func TestLiveLoginVerifyAndSearch(t *testing.T) {
	username := os.Getenv("QOBUZ_IT_USERNAME")
	password := os.Getenv("QOBUZ_IT_PASSWORD")
	if username == "" || password == "" {
		t.Skip("set QOBUZ_IT_USERNAME and QOBUZ_IT_PASSWORD to run live integration test")
	}

	appID := os.Getenv("QOBUZ_IT_APP_ID")
	if appID == "" {
		appID = "312369995"
	}
	appSecret := os.Getenv("QOBUZ_IT_APP_SECRET")
	if appSecret == "" {
		appSecret = "e79f8b9be485692b0e5f9dd895826368"
	}

	ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
	defer cancel()

	c := NewClient(appID, appSecret)
	if err := c.Login(ctx, username, password); err != nil {
		t.Fatalf("login failed: %v", err)
	}
	if c.token == "" {
		t.Fatalf("login succeeded but token is empty")
	}

	if err := c.VerifyAuth(ctx); err != nil {
		t.Fatalf("verify auth failed: %v", err)
	}

	tracks, err := c.SearchTracks(ctx, "Daft Punk One More Time", 5)
	if err != nil {
		t.Fatalf("search failed: %v", err)
	}
	if len(tracks) == 0 {
		t.Fatalf("search returned no results")
	}
}
