//go:build integration
// +build integration

package integration

import (
	"net/http"
	"testing"
)

func TestIntegration_Jobs(t *testing.T) {
	client := newTestClient()
	companyID, userID := setupTestCompanyAndUser(t)
	defer cleanupTestData() // Clean up after test

	t.Run("Create Job - Unauthorized", func(t *testing.T) {
		client.setAuthToken("")
		resp, err := client.post("/api/v1/jobs", map[string]interface{}{
			"title": "Unauthorized Job",
		})
		if err != nil {
			t.Fatalf("Request failed: %v", err)
		}
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusUnauthorized {
			t.Errorf("Expected 401, got %d", resp.StatusCode)
		}
	})

	t.Run("Create Job - Authorized", func(t *testing.T) {
		token := createAuthToken(t, userID, companyID, []string{"admin"})
		client.setAuthToken(token)

		jobPayload := map[string]interface{}{
			"companyId":      companyID,
			"title":          "Integration Test Job",
			"description":    "A job created by integration test with sufficient length",
			"salaryMin":      5000,
			"salaryMax":      8000,
			"salaryType":     "monthly",
			"employmentType": "full-time",
			"workingHours":   "flexible",
			"location":       "Remote",
			"status":         "published",
		}

		resp, err := client.post("/api/v1/jobs", jobPayload)
		if err != nil {
			t.Fatalf("Request failed: %v", err)
		}
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
			t.Errorf("Expected 201/200, got %d", resp.StatusCode)
		}
	})

	t.Run("List Jobs - Public", func(t *testing.T) {
		client.setAuthToken("")
		resp, err := client.get("/api/v1/jobs?limit=10")
		if err != nil {
			t.Fatalf("Request failed: %v", err)
		}
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusOK {
			t.Errorf("Expected 200, got %d", resp.StatusCode)
		}
	})
}
