using Netina.Domain.Entities.Brands;

namespace Netina.Repository.Handlers.Brands;

public class CreateBrandCommandHandler : IRequestHandler<CreateBrandCommand , BrandSDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public CreateBrandCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<BrandSDto> Handle(CreateBrandCommand request, CancellationToken cancellationToken)
    {
        var ent = Brand.Create(request.PersianName,request.EnglishName, request.Description, request.HasSpecialPage, request.PageUrl);
        foreach (var file in request.Files)
        {
            ent.AddFile(file.Name, file.FileLocation, file.FileName, file.IsHeader, file.IsPrimary, file.FileType);
        }
        _repositoryWrapper.SetRepository<Brand>().Add(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return ent.AdaptToSDto();
    }
}