//go:build test
// +build test

package user

// Package user contains tests for guest user creation endpoints.
//
// Test Coverage:
//   - CreateGuestUserHandler: POST /api/user/guest - Create a new guest user
//
// Edge case tests for validation:
//   - Invalid phone number format
//   - Empty first name
//   - Name exceeds 50 character limit
//   - Invalid email format

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

// TestGuestUser_Create_InvalidPhone verifies that an invalid phone number returns 400 Bad Request.
func TestGuestUser_Create_InvalidPhone(t *testing.T) {
	resetTestData(t)

	reqBody := CreateGuestUserRequest{
		FirstName: "Test",
		LastName:  "User",
		Email:     "test@test.com",
		Phone:     "not-a-phone",
	}
	body, _ := json.Marshal(reqBody)

	req := httptest.NewRequest(http.MethodPost, "/api/user/guest", bytes.NewReader(body))
	req.Header.Set("Content-Type", "application/json")

	rr := httptest.NewRecorder()
	CreateGuestUserHandler(rr, req)

	if rr.Code != http.StatusBadRequest {
		t.Errorf("expected status 400, got %d", rr.Code)
		t.Logf("response body: %s", rr.Body.String())
	}
}

// TestGuestUser_Create_EmptyFirstName verifies that an empty first name returns 400 Bad Request.
func TestGuestUser_Create_EmptyFirstName(t *testing.T) {
	resetTestData(t)

	reqBody := CreateGuestUserRequest{
		FirstName: "",
		LastName:  "User",
		Email:     "test@test.com",
		Phone:     "07123456789",
	}
	body, _ := json.Marshal(reqBody)

	req := httptest.NewRequest(http.MethodPost, "/api/user/guest", bytes.NewReader(body))
	req.Header.Set("Content-Type", "application/json")

	rr := httptest.NewRecorder()
	CreateGuestUserHandler(rr, req)

	if rr.Code != http.StatusBadRequest {
		t.Errorf("expected status 400, got %d", rr.Code)
		t.Logf("response body: %s", rr.Body.String())
	}
}

// TestGuestUser_Create_NameTooLong verifies that a first name exceeding 50 characters returns 400 Bad Request.
func TestGuestUser_Create_NameTooLong(t *testing.T) {
	resetTestData(t)

	reqBody := CreateGuestUserRequest{
		FirstName: strings.Repeat("a", 51),
		LastName:  "User",
		Email:     "test@test.com",
		Phone:     "07123456789",
	}
	body, _ := json.Marshal(reqBody)

	req := httptest.NewRequest(http.MethodPost, "/api/user/guest", bytes.NewReader(body))
	req.Header.Set("Content-Type", "application/json")

	rr := httptest.NewRecorder()
	CreateGuestUserHandler(rr, req)

	if rr.Code != http.StatusBadRequest {
		t.Errorf("expected status 400, got %d", rr.Code)
		t.Logf("response body: %s", rr.Body.String())
	}
}

// TestGuestUser_Create_InvalidEmail verifies that an invalid email format returns 400 Bad Request.
func TestGuestUser_Create_InvalidEmail(t *testing.T) {
	resetTestData(t)

	reqBody := CreateGuestUserRequest{
		FirstName: "Test",
		LastName:  "User",
		Email:     "not-an-email",
		Phone:     "07123456789",
	}
	body, _ := json.Marshal(reqBody)

	req := httptest.NewRequest(http.MethodPost, "/api/user/guest", bytes.NewReader(body))
	req.Header.Set("Content-Type", "application/json")

	rr := httptest.NewRecorder()
	CreateGuestUserHandler(rr, req)

	if rr.Code != http.StatusBadRequest {
		t.Errorf("expected status 400, got %d", rr.Code)
		t.Logf("response body: %s", rr.Body.String())
	}
}
