package handlers

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

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

// ============================================================================
// ReportingHandlers — Missing Tenant ID Tests (no store required)
// ============================================================================

func TestReportingHandlers_GetExecutiveReport_MissingTenantID(t *testing.T) {
	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/executive", nil)

	handler.GetExecutiveReport(c)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected status 400 for missing tenant ID, got %d", w.Code)
	}

	var resp map[string]interface{}
	if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
		t.Fatalf("Failed to parse response: %v", err)
	}
	if _, ok := resp["error"]; !ok {
		t.Error("Expected error field in response")
	}
}

func TestReportingHandlers_GetComplianceScore_MissingTenantID(t *testing.T) {
	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/score", nil)

	handler.GetComplianceScore(c)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected status 400 for missing tenant ID, got %d", w.Code)
	}

	var resp map[string]interface{}
	if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
		t.Fatalf("Failed to parse response: %v", err)
	}
	if _, ok := resp["error"]; !ok {
		t.Error("Expected error field in response")
	}
}

func TestReportingHandlers_GetUpcomingDeadlines_MissingTenantID(t *testing.T) {
	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/deadlines", nil)

	handler.GetUpcomingDeadlines(c)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected status 400 for missing tenant ID, got %d", w.Code)
	}

	var resp map[string]interface{}
	if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
		t.Fatalf("Failed to parse response: %v", err)
	}
	if _, ok := resp["error"]; !ok {
		t.Error("Expected error field in response")
	}
}

func TestReportingHandlers_GetRiskOverview_MissingTenantID(t *testing.T) {
	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/risks", nil)

	handler.GetRiskOverview(c)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected status 400 for missing tenant ID, got %d", w.Code)
	}

	var resp map[string]interface{}
	if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
		t.Fatalf("Failed to parse response: %v", err)
	}
	if _, ok := resp["error"]; !ok {
		t.Error("Expected error field in response")
	}
}

// ============================================================================
// ReportingHandlers — With Tenant ID (skip if no DB store)
// ============================================================================

func TestReportingHandlers_GetExecutiveReport_WithTenant(t *testing.T) {
	t.Skipf("Skipping integration test — requires live DB store")

	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/executive", nil)
	c.Set("tenant_id", uuid.New())
	c.Set("user_id", uuid.New())

	handler.GetExecutiveReport(c)

	if w.Code != http.StatusOK {
		t.Errorf("Expected status 200, got %d", w.Code)
	}
}

func TestReportingHandlers_GetComplianceScore_WithTenant(t *testing.T) {
	t.Skipf("Skipping integration test — requires live DB store")

	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/score", nil)
	c.Set("tenant_id", uuid.New())
	c.Set("user_id", uuid.New())

	handler.GetComplianceScore(c)

	if w.Code != http.StatusOK {
		t.Errorf("Expected status 200, got %d", w.Code)
	}
}

func TestReportingHandlers_GetUpcomingDeadlines_WithTenant(t *testing.T) {
	t.Skipf("Skipping integration test — requires live DB store")

	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/deadlines", nil)
	c.Set("tenant_id", uuid.New())
	c.Set("user_id", uuid.New())

	handler.GetUpcomingDeadlines(c)

	if w.Code != http.StatusOK {
		t.Errorf("Expected status 200, got %d", w.Code)
	}
}

func TestReportingHandlers_GetRiskOverview_WithTenant(t *testing.T) {
	t.Skipf("Skipping integration test — requires live DB store")

	handler := &ReportingHandlers{store: nil}

	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	c.Request = httptest.NewRequest("GET", "/reporting/risks", nil)
	c.Set("tenant_id", uuid.New())
	c.Set("user_id", uuid.New())

	handler.GetRiskOverview(c)

	if w.Code != http.StatusOK {
		t.Errorf("Expected status 200, got %d", w.Code)
	}
}
