package user

import (
	"context"
	"errors"

	"github.com/rede5/gohorsejobs/backend/internal/core/ports"
)

type DeleteUserUseCase struct {
	userRepo ports.UserRepository
}

func NewDeleteUserUseCase(uRepo ports.UserRepository) *DeleteUserUseCase {
	return &DeleteUserUseCase{
		userRepo: uRepo,
	}
}

func (uc *DeleteUserUseCase) Execute(ctx context.Context, userID, tenantID string) error {
	// 1. Verify User exists and belongs to Tenant
	user, err := uc.userRepo.FindByID(ctx, userID)
	if err != nil {
		return err
	}

	// 2. Check Permission (Tenant Check)
	// If tenantID is empty, it means SuperAdmin or Admin (handler logic), so we skip check.
	if tenantID != "" && user.TenantID != tenantID {
		return errors.New("user not found in this tenant")
	}

	// 3. Delete
	return uc.userRepo.Delete(ctx, userID)
}
