package user

import (
	"context"
	"errors"

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

type UpdateUserUseCase struct {
	userRepo ports.UserRepository
}

func NewUpdateUserUseCase(uRepo ports.UserRepository) *UpdateUserUseCase {
	return &UpdateUserUseCase{
		userRepo: uRepo,
	}
}

func (uc *UpdateUserUseCase) Execute(ctx context.Context, id, tenantID string, input dto.UpdateUserRequest) (*dto.UserResponse, error) {
	// 1. Find User
	user, err := uc.userRepo.FindByID(ctx, id)
	if err != nil {
		return nil, err
	}
	if user == nil {
		return nil, errors.New("user not found")
	}

	// 2. Check Permission (Tenant Check)
	if user.TenantID != tenantID {
		return nil, errors.New("forbidden: user belongs to another tenant")
	}

	// 3. Update Fields
	if input.Name != nil {
		user.Name = *input.Name
	}
	if input.Email != nil {
		user.Email = *input.Email
	}
	if input.Active != nil {
		if *input.Active {
			user.Status = "ACTIVE"
		} else {
			user.Status = "INACTIVE"
		}
	}

	// 4. Save
	updated, err := uc.userRepo.Update(ctx, user)
	if err != nil {
		return nil, err
	}

	// 5. Convert to Response
	roles := make([]string, len(updated.Roles))
	for i, r := range updated.Roles {
		roles[i] = r.Name
	}

	return &dto.UserResponse{
		ID:        updated.ID,
		Name:      updated.Name,
		Email:     updated.Email,
		Roles:     roles,
		Status:    updated.Status,
		CreatedAt: updated.CreatedAt,
	}, nil
}
