package servicemocks

import (
	"context"
	"time"

	"veza-backend-api/internal/services"

	"github.com/google/uuid"
	"github.com/stretchr/testify/mock"
)

// MockSessionService est un mock pour SessionService (T0042)
type MockSessionService struct {
	mock.Mock
}

// NewMockSessionService crée un nouveau mock SessionService (T0042)
func NewMockSessionService() *MockSessionService {
	return &MockSessionService{}
}

// CreateSession mock (T0042)
func (m *MockSessionService) CreateSession(ctx context.Context, req *services.SessionCreateRequest) (*services.Session, error) {
	args := m.Called(ctx, req)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*services.Session), args.Error(1)
}

// ValidateSession mock (T0042)
func (m *MockSessionService) ValidateSession(ctx context.Context, token string) (*services.Session, error) {
	args := m.Called(ctx, token)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*services.Session), args.Error(1)
}

// RevokeSession mock (T0042)
func (m *MockSessionService) RevokeSession(ctx context.Context, token string) error {
	args := m.Called(ctx, token)
	return args.Error(0)
}

// RevokeAllUserSessions mock (T0042)
func (m *MockSessionService) RevokeAllUserSessions(ctx context.Context, userID uuid.UUID) (int64, error) {
	args := m.Called(ctx, userID)
	return args.Get(0).(int64), args.Error(1)
}

// GetUserSessions mock (T0042)
func (m *MockSessionService) GetUserSessions(ctx context.Context, userID uuid.UUID) ([]*services.Session, error) {
	args := m.Called(ctx, userID)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]*services.Session), args.Error(1)
}

// CleanupExpiredSessions mock (T0042)
func (m *MockSessionService) CleanupExpiredSessions(ctx context.Context) (int64, error) {
	args := m.Called(ctx)
	return args.Get(0).(int64), args.Error(1)
}

// RefreshSession mock (T0042)
func (m *MockSessionService) RefreshSession(ctx context.Context, token string, newExpiresIn time.Duration) error {
	args := m.Called(ctx, token, newExpiresIn)
	return args.Error(0)
}

// GetSessionStats mock (T0042)
func (m *MockSessionService) GetSessionStats(ctx context.Context) (map[string]interface{}, error) {
	args := m.Called(ctx)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(map[string]interface{}), args.Error(1)
}

// MockAuditService est un mock pour AuditService (T0042)
type MockAuditService struct {
	mock.Mock
}

// NewMockAuditService crée un nouveau mock AuditService (T0042)
func NewMockAuditService() *MockAuditService {
	return &MockAuditService{}
}

// LogAction mock (T0042)
func (m *MockAuditService) LogAction(ctx context.Context, req *services.AuditLogCreateRequest) error {
	args := m.Called(ctx, req)
	return args.Error(0)
}

// LogLogin mock (T0042)
func (m *MockAuditService) LogLogin(ctx context.Context, userID *uuid.UUID, success bool, ipAddress, userAgent string, metadata map[string]interface{}) error {
	args := m.Called(ctx, userID, success, ipAddress, userAgent, metadata)
	return args.Error(0)
}

// LogLogout mock (T0042)
func (m *MockAuditService) LogLogout(ctx context.Context, userID uuid.UUID, ipAddress, userAgent string) error {
	args := m.Called(ctx, userID, ipAddress, userAgent)
	return args.Error(0)
}

// LogUpload mock (T0042)
func (m *MockAuditService) LogUpload(ctx context.Context, userID uuid.UUID, resourceID uuid.UUID, fileName string, fileSize int64, ipAddress, userAgent string) error {
	args := m.Called(ctx, userID, resourceID, fileName, fileSize, ipAddress, userAgent)
	return args.Error(0)
}

// LogPermissionChange mock (T0042)
func (m *MockAuditService) LogPermissionChange(ctx context.Context, userID uuid.UUID, targetUserID uuid.UUID, oldPermissions, newPermissions []string, ipAddress, userAgent string) error {
	args := m.Called(ctx, userID, targetUserID, oldPermissions, newPermissions, ipAddress, userAgent)
	return args.Error(0)
}

// LogDeletion mock (T0042)
func (m *MockAuditService) LogDeletion(ctx context.Context, userID uuid.UUID, resource string, resourceID uuid.UUID, ipAddress, userAgent string) error {
	args := m.Called(ctx, userID, resource, resourceID, ipAddress, userAgent)
	return args.Error(0)
}

// SearchLogs mock (T0042)
func (m *MockAuditService) SearchLogs(ctx context.Context, req *services.AuditLogSearchRequest) ([]*services.AuditLog, error) {
	args := m.Called(ctx, req)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]*services.AuditLog), args.Error(1)
}

// GetStats mock (T0042)
func (m *MockAuditService) GetStats(ctx context.Context, startDate, endDate time.Time) ([]*services.AuditStats, error) {
	args := m.Called(ctx, startDate, endDate)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]*services.AuditStats), args.Error(1)
}

// SetupMockSessionSuccess configure un mock pour succès de création de session (T0042)
func SetupMockSessionSuccess(mockService *MockSessionService, userID uuid.UUID) {
	session := &services.Session{
		ID:        uuid.New(),
		UserID:    userID,
		CreatedAt: time.Now(),
		ExpiresAt: time.Now().Add(24 * time.Hour),
		IPAddress: "127.0.0.1",
		UserAgent: "test-agent",
	}
	// Utiliser mock.MatchedBy pour matcher n'importe quel req avec le bon userID
	mockService.On("CreateSession", mock.Anything, mock.MatchedBy(func(req *services.SessionCreateRequest) bool {
		return req != nil && req.UserID == userID
	})).Return(session, nil)
}

// SetupMockSessionValidationSuccess configure un mock pour validation de session réussie (T0042)
func SetupMockSessionValidationSuccess(mockService *MockSessionService, userID uuid.UUID, token string) {
	session := &services.Session{
		ID:        uuid.New(),
		UserID:    userID,
		CreatedAt: time.Now(),
		ExpiresAt: time.Now().Add(24 * time.Hour),
		IPAddress: "127.0.0.1",
	}
	mockService.On("ValidateSession", mock.Anything, token).Return(session, nil)
}

// SetupMockSessionValidationError configure un mock pour erreur de validation de session (T0042)
func SetupMockSessionValidationError(mockService *MockSessionService, token string, err error) {
	mockService.On("ValidateSession", mock.Anything, token).Return(nil, err)
}

// SetupMockSessionRevokeSuccess configure un mock pour révocation de session réussie (T0042)
func SetupMockSessionRevokeSuccess(mockService *MockSessionService, token string) {
	mockService.On("RevokeSession", mock.Anything, token).Return(nil)
}

// SetupMockAuditSuccess configure un mock audit pour succès (T0042)
func SetupMockAuditSuccess(mockService *MockAuditService) {
	mockService.On("LogAction", mock.Anything, mock.Anything).Return(nil)
	mockService.On("LogLogin", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
	mockService.On("LogLogout", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
	mockService.On("LogUpload", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
	mockService.On("LogPermissionChange", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
	mockService.On("LogDeletion", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
}

// SetupMockAuditLogActionSuccess configure un mock pour LogAction spécifique (T0042)
func SetupMockAuditLogActionSuccess(mockService *MockAuditService, req *services.AuditLogCreateRequest) {
	mockService.On("LogAction", mock.Anything, req).Return(nil)
}

// SetupMockAuditLogLoginSuccess configure un mock pour LogLogin spécifique (T0042)
func SetupMockAuditLogLoginSuccess(mockService *MockAuditService, userID *uuid.UUID, success bool) {
	mockService.On("LogLogin", mock.Anything, userID, success, mock.Anything, mock.Anything, mock.Anything).Return(nil)
}

// SetupMockAuditSearchLogs configure un mock pour SearchLogs (T0042)
func SetupMockAuditSearchLogs(mockService *MockAuditService, req *services.AuditLogSearchRequest, logs []*services.AuditLog) {
	mockService.On("SearchLogs", mock.Anything, req).Return(logs, nil)
}

// SetupMockAuditSearchLogsError configure un mock pour erreur de recherche (T0042)
func SetupMockAuditSearchLogsError(mockService *MockAuditService, req *services.AuditLogSearchRequest, err error) {
	mockService.On("SearchLogs", mock.Anything, req).Return(nil, err)
}

// SetupMockAuditLogLogoutSuccess configure un mock pour LogLogout spécifique (T0042)
func SetupMockAuditLogLogoutSuccess(mockService *MockAuditService, userID uuid.UUID) {
	mockService.On("LogLogout", mock.Anything, userID, mock.Anything, mock.Anything).Return(nil)
}

// SetupMockAuditLogUploadSuccess configure un mock pour LogUpload spécifique (T0042)
func SetupMockAuditLogUploadSuccess(mockService *MockAuditService, userID, resourceID uuid.UUID) {
	mockService.On("LogUpload", mock.Anything, userID, resourceID, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
}

// SetupMockAuditLogPermissionChangeSuccess configure un mock pour LogPermissionChange spécifique (T0042)
func SetupMockAuditLogPermissionChangeSuccess(mockService *MockAuditService, userID, targetUserID uuid.UUID) {
	mockService.On("LogPermissionChange", mock.Anything, userID, targetUserID, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
}

// SetupMockAuditLogDeletionSuccess configure un mock pour LogDeletion spécifique (T0042)
func SetupMockAuditLogDeletionSuccess(mockService *MockAuditService, userID, resourceID uuid.UUID) {
	mockService.On("LogDeletion", mock.Anything, userID, mock.Anything, resourceID, mock.Anything, mock.Anything).Return(nil)
}
