using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Domain.Entities.Blog;
using MediatR;

namespace CMSMicroservice.Application.BlogCategoryCQ.Commands.CreateBlogCategory;

public class CreateBlogCategoryCommandHandler : IRequestHandler<CreateBlogCategoryCommand, long>
{
    private readonly IApplicationDbContext _context;

    public CreateBlogCategoryCommandHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<long> Handle(CreateBlogCategoryCommand request, CancellationToken cancellationToken)
    {
        var entity = new BlogCategory
        {
            Title = request.Title,
            Slug = request.Slug.ToLower(),
            Description = request.Description,
            IconName = request.IconName,
            SortOrder = request.SortOrder,
            IsActive = request.IsActive
        };

        _context.BlogCategories.Add(entity);
        await _context.SaveChangesAsync(cancellationToken);

        return entity.Id;
    }
}
