package handlers

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestHandleTuneInReport(t *testing.T) {
	r, _ := setupRouter("http://localhost:8001", nil)

	ts := httptest.NewServer(r)
	defer ts.Close()

	t.Run("START event", func(t *testing.T) {
		payload := `{"timeStamp":"2026-03-29T21:33:04+0000","eventType":"START","reason":"USER_SELECT_PLAYABLE","timeIntoTrack":0,"playbackDelay":7419}`
		req, _ := http.NewRequest("POST", ts.URL+"/bmx/tunein/v1/report?stream_id=e536753726&guide_id=s166521&listen_id=1774819980&stream_type=liveRadio", strings.NewReader(payload))
		req.Header.Set("Authorization", "Bearer mock-token")
		req.Header.Set("Content-Type", "application/json")

		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		defer res.Body.Close()

		if res.StatusCode != http.StatusOK {
			t.Errorf("Expected status 200, got %v", res.Status)
		}

		var resp map[string]interface{}
		if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
			t.Fatal(err)
		}

		if resp["nextReportIn"] != float64(1800) {
			t.Errorf("Expected nextReportIn 1800, got %v", resp["nextReportIn"])
		}
		links := resp["_links"].(map[string]interface{})
		self := links["self"].(map[string]interface{})
		if !strings.Contains(self["href"].(string), "/v1/report") {
			t.Errorf("Expected href to contain /v1/report, got %v", self["href"])
		}
	})

	t.Run("STOP event", func(t *testing.T) {
		payload := `{"timeStamp":"2026-03-29T21:33:44+0000","eventType":"STOP","reason":"USER_STOP","timeIntoTrack":39,"playbackDelay":0}`
		req, _ := http.NewRequest("POST", ts.URL+"/bmx/tunein/v1/report?stream_id=e536753726&guide_id=s166521&listen_id=1774819980&last_titt=0&duration_balance=0&stream_type=liveRadio", strings.NewReader(payload))
		req.Header.Set("Authorization", "Bearer mock-token")
		req.Header.Set("Content-Type", "application/json")

		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		defer res.Body.Close()

		if res.StatusCode != http.StatusOK {
			t.Errorf("Expected status 200, got %v", res.Status)
		}

		var resp map[string]interface{}
		if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
			t.Fatal(err)
		}

		if len(resp) != 0 {
			t.Errorf("Expected empty response object, got %v", resp)
		}
	})

	t.Run("Unauthorized", func(t *testing.T) {
		t.Skip("auth gate temporarily disabled in handlers_bmx_tunein.go; restore this assertion when the gate is re-enabled")

		req, _ := http.NewRequest("POST", ts.URL+"/bmx/tunein/v1/report", nil)
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		defer res.Body.Close()

		if res.StatusCode != http.StatusUnauthorized {
			t.Errorf("Expected status 401, got %v", res.Status)
		}
	})
}
