using EnotaryoPH.Data.Entities;

namespace EnotaryoPH.Web.Common.Models
{
    internal static class IdentificationDocumentModelMapper
    {
        internal static IdentificationDocument ToEntity(this IdentificationDocumentModel model, int userID)
        {
            var now = DateTime.UtcNow;
            var entity = new IdentificationDocument
            {
                UploadedOn = now,
                UserID = userID,
                IdentificationDocument_UID = Guid.CreateVersion7(now),
                CreatedOn = now
            };

            return model.ToEntity(entity);
        }

        internal static IdentificationDocument ToEntity(this IdentificationDocumentModel model, IdentificationDocument entity)
        {
            ArgumentNullException.ThrowIfNull(model.File);

            entity.ExpirationDate = model.ExpirationDate.ToUTC();
            entity.DateIssued = model.DateIssued.ToUTC();
            entity.PlaceIssued = model.PlaceIssued;
            entity.IdNumber = model.IdNumber;
            entity.Type = model.IdentificationType;

            var file = model.File;
            entity.Filename = file.FileName;
            var stream = new MemoryStream((int)file.Length);
            file.CopyTo(stream);
            entity.File = stream.ToArray();
            return entity;
        }
    }
}