namespace MyOffice.Services.Verifications;

using MyOffice.Core.Helpers;
using MyOffice.Data.Models.Users;
using MyOffice.Data.Models.Verifications;
using MyOffice.Data.Repositories.Item;

public class VerificationService
{
	private readonly TimeSpan _defaultExpires = TimeSpan.FromDays(1);

	private readonly IVerificationCodeRepository _verificationCodeRepository;

	public VerificationService(
		IVerificationCodeRepository verificationCodeRepository
	)
	{
		_verificationCodeRepository = verificationCodeRepository;
	}

	public VerificationCode Add(
		User user,
		string destination,
		VerificationCodeTemplateEnum template,
		VerificationCodeTypeEnum type,
		DateTime? expiresOn = null,
		string? metadata = null
	)
	{
		if (user == null)
			throw new ArgumentNullException(nameof(user));
		if (destination == null)
			throw new ArgumentNullException(nameof(destination));

		var verificationCode = new VerificationCode
		{
			UserId = user.Id,
			Code = RandomizationHelper.Generate(40),
			CreatedOn = DateTime.UtcNow,
			ExpiresOn = expiresOn ?? DateTime.UtcNow.Add(_defaultExpires),
			Template = template.ToString(),
			DestinationType = type.ToString(),
			Destination = destination,
			Metadata = metadata,
		};

		_verificationCodeRepository.Add(verificationCode);

		return verificationCode;
	}
}