package service

import (
	"baron-sso-backend/internal/domain"
	"context"

	"github.com/stretchr/testify/mock"
	"gorm.io/gorm"
)

// --- Shared Mocks for Service Tests ---

type MockKetoOutboxRepositoryShared struct {
	mock.Mock
}

func (m *MockKetoOutboxRepositoryShared) Create(ctx context.Context, entry *domain.KetoOutbox) error {
	return m.Called(ctx, entry).Error(0)
}

func (m *MockKetoOutboxRepositoryShared) CreateWithTx(tx *gorm.DB, entry *domain.KetoOutbox) error {
	return m.Called(tx, entry).Error(0)
}

func (m *MockKetoOutboxRepositoryShared) FindPending(ctx context.Context, limit int) ([]domain.KetoOutbox, error) {
	args := m.Called(ctx, limit)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.KetoOutbox), args.Error(1)
}

func (m *MockKetoOutboxRepositoryShared) ListCurrentBySubject(ctx context.Context, namespace, subject string) ([]domain.KetoOutbox, error) {
	args := m.Called(ctx, namespace, subject)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.KetoOutbox), args.Error(1)
}

func (m *MockKetoOutboxRepositoryShared) UpdateStatus(ctx context.Context, id string, status string, retryCount int, lastError string) error {
	return m.Called(ctx, id, status, retryCount, lastError).Error(0)
}

func (m *MockKetoOutboxRepositoryShared) MarkProcessed(ctx context.Context, id string) error {
	return m.Called(ctx, id).Error(0)
}

type MockKetoServiceShared struct {
	mock.Mock
}

func (m *MockKetoServiceShared) CheckPermission(ctx context.Context, subject, namespace, object, relation string) (bool, error) {
	args := m.Called(ctx, subject, namespace, object, relation)
	return args.Bool(0), args.Error(1)
}

func (m *MockKetoServiceShared) CreateRelation(ctx context.Context, namespace, object, relation, subject string) error {
	args := m.Called(ctx, namespace, object, relation, subject)
	return args.Error(0)
}

func (m *MockKetoServiceShared) DeleteRelation(ctx context.Context, namespace, object, relation, subject string) error {
	args := m.Called(ctx, namespace, object, relation, subject)
	return args.Error(0)
}

func (m *MockKetoServiceShared) ListRelations(ctx context.Context, namespace, object, relation, subject string) ([]RelationTuple, error) {
	args := m.Called(ctx, namespace, object, relation, subject)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]RelationTuple), args.Error(1)
}

func (m *MockKetoServiceShared) ListObjects(ctx context.Context, namespace, relation, subject string) ([]string, error) {
	args := m.Called(ctx, namespace, relation, subject)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]string), args.Error(1)
}

type MockKratosAdminServiceShared struct {
	mock.Mock
}

func (m *MockKratosAdminServiceShared) ListIdentities(ctx context.Context) ([]KratosIdentity, error) {
	args := m.Called(ctx)
	return args.Get(0).([]KratosIdentity), args.Error(1)
}

func (m *MockKratosAdminServiceShared) FindIdentityIDByIdentifier(ctx context.Context, identifier string) (string, error) {
	args := m.Called(ctx, identifier)
	return args.String(0), args.Error(1)
}

func (m *MockKratosAdminServiceShared) GetIdentity(ctx context.Context, identityID string) (*KratosIdentity, error) {
	args := m.Called(ctx, identityID)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*KratosIdentity), args.Error(1)
}

func (m *MockKratosAdminServiceShared) UpdateIdentity(ctx context.Context, identityID string, traits map[string]any, state string) (*KratosIdentity, error) {
	args := m.Called(ctx, identityID, traits, state)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*KratosIdentity), args.Error(1)
}

func (m *MockKratosAdminServiceShared) UpdateIdentityPassword(ctx context.Context, identityID, newPassword string) error {
	args := m.Called(ctx, identityID, newPassword)
	return args.Error(0)
}

func (m *MockKratosAdminServiceShared) DeleteIdentity(ctx context.Context, identityID string) error {
	args := m.Called(ctx, identityID)
	return args.Error(0)
}

func (m *MockKratosAdminServiceShared) CreateUser(ctx context.Context, user *domain.BrokerUser, password string) (string, error) {
	args := m.Called(ctx, user, password)
	return args.String(0), args.Error(1)
}

func (m *MockKratosAdminServiceShared) ListIdentitySessions(ctx context.Context, identityID string) ([]KratosSession, error) {
	return nil, nil
}

func (m *MockKratosAdminServiceShared) GetSession(ctx context.Context, sessionID string) (*KratosSession, error) {
	return nil, nil
}

func (m *MockKratosAdminServiceShared) DeleteSession(ctx context.Context, sessionID string) error {
	return nil
}
