package handlers

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

	"github.com/orchard9/rdev/internal/sdlc"
)

func TestSDLCHandler_CreateBranch(t *testing.T) {
	exec := &testSDLCExecutor{}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodPost, "/projects/test-project/sdlc/features/auth-flow/branches", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	if w.Code != http.StatusCreated {
		t.Errorf("expected status 201, got %d: %s", w.Code, w.Body.String())
	}
}

func TestSDLCHandler_CreateBranch_FeatureNotFound(t *testing.T) {
	exec := &testSDLCExecutor{err: sdlc.ErrFeatureNotFound}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodPost, "/projects/test-project/sdlc/features/nonexistent/branches", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Errorf("expected status 404, got %d: %s", w.Code, w.Body.String())
	}
}

func TestSDLCHandler_CreateBranch_AlreadyExists(t *testing.T) {
	exec := &testSDLCExecutor{err: sdlc.ErrBranchExists}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodPost, "/projects/test-project/sdlc/features/auth-flow/branches", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

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

func TestSDLCHandler_GetBranchStatus(t *testing.T) {
	exec := &testSDLCExecutor{}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodGet, "/projects/test-project/sdlc/features/auth-flow/branches", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected status 200, got %d: %s", w.Code, w.Body.String())
	}
}

func TestSDLCHandler_GetBranchStatus_NotFound(t *testing.T) {
	exec := &testSDLCExecutor{err: sdlc.ErrBranchNotFound}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodGet, "/projects/test-project/sdlc/features/auth-flow/branches", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Errorf("expected status 404, got %d: %s", w.Code, w.Body.String())
	}
}

func TestSDLCHandler_SyncBranch(t *testing.T) {
	exec := &testSDLCExecutor{}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodPost, "/projects/test-project/sdlc/features/auth-flow/branches/sync", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected status 200, got %d: %s", w.Code, w.Body.String())
	}
}

func TestSDLCHandler_SyncBranch_BranchNotFound(t *testing.T) {
	exec := &testSDLCExecutor{err: sdlc.ErrBranchNotFound}
	_, router := setupSDLCHandler(exec)

	req := httptest.NewRequest(http.MethodPost, "/projects/test-project/sdlc/features/auth-flow/branches/sync", nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Errorf("expected status 404, got %d: %s", w.Code, w.Body.String())
	}
}
