<?php

namespace MintyPHP\Service\User;

use MintyPHP\Repository\User\UserWriteRepositoryInterface;

class UserPasswordService
{
    public function __construct(
        private readonly UserPasswordPolicyService $passwordPolicyService,
        private readonly UserWriteRepositoryInterface $userWriteRepository
    ) {
    }

    public function validatePassword(
        string $password,
        string $password2,
        bool $required,
        ?string $email
    ): array {
        return $this->passwordPolicyService->validate($password, $password2, $required, $email);
    }

    public function passwordHints(): array
    {
        return $this->passwordPolicyService->hints();
    }

    public function passwordMinLength(): int
    {
        return $this->passwordPolicyService->minLength();
    }

    public function resetPassword(int $userId, string $password, string $password2): array
    {
        $errors = $this->passwordPolicyService->validate($password, $password2, true, '');
        if ($errors) {
            return ['ok' => false, 'errors' => $errors];
        }

        $updated = $this->userWriteRepository->setPassword($userId, $password);
        if (!$updated) {
            return ['ok' => false, 'errors' => [t('Password can not be updated')]];
        }

        return ['ok' => true];
    }
}
