using EnotaryoPH.Data.Entities;

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

            return model.ToEntity(entity);
        }

        internal static IdentificationDocument ToEntity(this IdentificationDocumentModel model, IdentificationDocument entity)
        {
            ArgumentNullException.ThrowIfNull(model.File, nameof(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;
        }
    }
}