package integration

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// ==============================================================================
// Query Tests with go-salesforce
// ==============================================================================

// TestGoSF_Query tests SOQL query via go-salesforce library
func TestGoSF_Query(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test")
	}

	sf := newGoSFClient(t)
	if sf == nil {
		t.Skip("go-salesforce client not available")
	}

	var accounts []Account
	err := sf.Query("SELECT Id, Name FROM Account LIMIT 5", &accounts)

	// Log the result regardless of error
	t.Logf("Query returned %d accounts, error: %v", len(accounts), err)

	// Verify query executed successfully - data varies in test environment
	assert.NoError(t, err, "Query should succeed")
}

// TestGoSF_QueryStruct tests typed query via go-salesforce library
// TestGoSF_ContactLifecycle tests the full lifecycle of a Contact, including its relationship to Account
func TestGoSF_ContactLifecycle(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test")
	}

	sf := newGoSFClient(t)
	if sf == nil {
		t.Skip("go-salesforce client not available")
	}

	// 1. Create Parent Account
	account := Account{
		Name: "Parent Account for Contact",
	}
	accResult, err := sf.InsertOne("Account", account)
	require.NoError(t, err, "Failed to create parent account")
	require.True(t, accResult.Success, "Account insert failed")

	t.Logf("Created Parent Account: %s", accResult.Id)

	// 2. Create Contact linked to Account
	contact := Contact{
		FirstName: "John",
		LastName:  "Doe",
		Email:     "john.doe@example.com",
		AccountId: accResult.Id, // Standard Relationship Field
	}
	conResult, err := sf.InsertOne("Contact", contact)
	require.NoError(t, err, "Failed to create contact")
	require.True(t, conResult.Success, "Contact insert failed")

	t.Logf("Created Contact: %s (Linked to %s)", conResult.Id, accResult.Id)

	// 3. Query Contact to verify fields and relationship
	var queriedContacts []Contact
	// Note: NexusCRM SOQL might case-sensitivity rules, standard generic SOQL uses proper casing
	err = sf.Query(fmt.Sprintf("SELECT Id, FirstName, LastName, AccountId FROM Contact WHERE Id = '%s'", conResult.Id), &queriedContacts)
	require.NoError(t, err, "Query failed")
	require.Len(t, queriedContacts, 1, "Should find exactly one contact")

	found := queriedContacts[0]
	assert.Equal(t, contact.FirstName, found.FirstName)
	assert.Equal(t, contact.LastName, found.LastName)
	assert.Equal(t, accResult.Id, found.AccountId, "Relationship field AccountId should match")

	// 4. Cleanup (Optional, but good for hygiene if not automated)
	// sf.DeleteOne("Contact", found)
	// sf.DeleteOne("Account", account)
}

// ==============================================================================
// Single Record DML Tests with go-salesforce
// ==============================================================================

// TestGoSF_InsertOne tests single record insert via go-salesforce library
func TestGoSF_InsertOne(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test")
	}

	sf := newGoSFClient(t)
	if sf == nil {
		t.Skip("go-salesforce client not available")
	}

	account := Account{
		Name: "GoSF Test Account",
	}

	result, err := sf.InsertOne("Account", account)
	t.Logf("InsertOne result: %+v, error: %v", result, err)

	require.NoError(t, err, "InsertOne should succeed")
	assert.NotEmpty(t, result.Id, "Should return created record ID")
	assert.True(t, result.Success, "Insert should be successful")
}

// TestGoSF_UpdateOne tests single record update via go-salesforce library
func TestGoSF_UpdateOne(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test")
	}

	sf := newGoSFClient(t)
	if sf == nil {
		t.Skip("go-salesforce client not available")
	}

	// First create a record to update
	account := Account{Name: "GoSF Update Test"}
	result, err := sf.InsertOne("Account", account)
	if err != nil {
		t.Skipf("Could not create test record: %v", err)
	}

	// Update the record
	account.Id = result.Id
	account.Name = "GoSF Updated Account"

	err = sf.UpdateOne("Account", account)
	t.Logf("UpdateOne error: %v", err)

	// Update should succeed if insert worked
	assert.NoError(t, err, "Update should succeed")
}

// TestGoSF_DeleteOne tests single record delete via go-salesforce library
func TestGoSF_DeleteOne(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test")
	}

	sf := newGoSFClient(t)
	if sf == nil {
		t.Skip("go-salesforce client not available")
	}

	// First create a record to delete
	account := Account{Name: "GoSF Delete Test"}
	result, err := sf.InsertOne("Account", account)
	if err != nil {
		t.Skipf("Could not create test record: %v", err)
	}

	// Delete the record
	account.Id = result.Id
	err = sf.DeleteOne("Account", account)
	t.Logf("DeleteOne error: %v", err)

	assert.NoError(t, err, "Delete should succeed")
}
