package tests

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

	"mohportal/config"
	"mohportal/handlers"

	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
)

func setupRouter() *gin.Engine {
	gin.SetMode(gin.TestMode)
	router := gin.New()
	
	// Health check endpoint
	router.GET("/health", handlers.HealthCheck)
	
	// API routes
	api := router.Group("/api/v1")
	{
		tenants := api.Group("/tenants")
		{
			tenants.POST("/", handlers.CreateTenant)
			tenants.GET("/", handlers.GetTenants)
			tenants.GET("/:id", handlers.GetTenant)
		}
		
		auth := api.Group("/auth")
		{
			auth.POST("/login", handlers.Login)
			auth.POST("/register", handlers.Register)
		}
		
		positions := api.Group("/positions")
		{
			positions.GET("/", handlers.GetPositions)
			positions.GET("/:id", handlers.GetPosition)
			positions.POST("/", handlers.CreatePosition)
		}
		
		applications := api.Group("/applications")
		{
			applications.GET("/", handlers.GetApplications)
			applications.POST("/", handlers.CreateApplication)
		}
		
		resumes := api.Group("/resumes")
		{
			resumes.POST("/", handlers.UploadResume)
			resumes.GET("/:id", handlers.GetResume)
		}
	}
	
	return router
}

func TestHealthCheck(t *testing.T) {
	router := setupRouter()
	
	req, _ := http.NewRequest("GET", "/health", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	
	assert.Equal(t, http.StatusOK, w.Code)
	
	var response map[string]interface{}
	err := json.Unmarshal(w.Body.Bytes(), &response)
	assert.NoError(t, err)
	assert.Equal(t, "healthy", response["status"])
}

func TestCreateTenant(t *testing.T) {
	router := setupRouter()
	
	tenantData := map[string]string{
		"name":        "Test Tenant",
		"slug":        "test-tenant",
		"description": "A test tenant for testing purposes",
	}
	
	jsonData, _ := json.Marshal(tenantData)
	req, _ := http.NewRequest("POST", "/api/v1/tenants/", bytes.NewBuffer(jsonData))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	
	// For now, just check that it doesn't return an internal server error
	// since we don't have a connected DB in testing
	assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}

func TestGetTenants(t *testing.T) {
	router := setupRouter()
	
	req, _ := http.NewRequest("GET", "/api/v1/tenants/", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	
	// For now, just check that it doesn't return an internal server error
	assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}

func TestCreateUser(t *testing.T) {
	router := setupRouter()
	
	userData := map[string]interface{}{
		"tenant_id":  "00000000-0000-0000-0000-000000000000", // dummy UUID
		"email":      "test@example.com",
		"username":   "testuser",
		"first_name": "Test",
		"last_name":  "User",
		"phone":      "1234567890",
		"role":       "job_seeker",
		"password":   "password123",
	}
	
	jsonData, _ := json.Marshal(userData)
	req, _ := http.NewRequest("POST", "/api/v1/auth/register", bytes.NewBuffer(jsonData))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	
	// For now, just check that it doesn't return an internal server error
	assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}

func TestLogin(t *testing.T) {
	router := setupRouter()
	
	loginData := map[string]string{
		"email":    "test@example.com",
		"password": "password123",
	}
	jsonData, _ := json.Marshal(loginData)
	
	req, _ := http.NewRequest("POST", "/api/v1/auth/login", bytes.NewBuffer(jsonData))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	
	// For now, just check that it doesn't return an internal server error
	assert.NotEqual(t, http.StatusInternalServerError, w.Code)
}