namespace BackOffice.Services.Blog;

public class BlogCategoryService : IBlogCategoryService
{
    private readonly CMSMicroservice.Protobuf.Protos.BlogCategory.BlogCategoryContract.BlogCategoryContractClient _client;

    public BlogCategoryService(CMSMicroservice.Protobuf.Protos.BlogCategory.BlogCategoryContract.BlogCategoryContractClient client)
    {
        _client = client;
    }

    public async Task<BlogCategoryListResultDto> GetAllAsync(BlogCategoryFilterDto filter)
    {
        var request = new CMSMicroservice.Protobuf.Protos.BlogCategory.GetAllBlogCategoriesRequest
        {
            PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
            {
                PageNumber = filter.PageNumber,
                PageSize = filter.PageSize
            }
        };

        var response = await _client.GetAllBlogCategoriesAsync(request);

        var result = new BlogCategoryListResultDto
        {
            TotalCount = response.MetaData != null ? (int)response.MetaData.TotalCount : response.Models.Count
        };

        foreach (var m in response.Models)
        {
            result.Items.Add(new BlogCategoryItemDto
            {
                Id = m.Id,
                Title = m.Title,
                Slug = m.Slug,
                Description = m.Description,
                IconName = m.IconName,
                SortOrder = m.SortOrder,
                IsActive = m.IsActive,
                PostCount = m.PostCount
            });
        }

        return result;
    }

    public async Task<List<BlogCategoryItemDto>> GetActiveAsync()
    {
        var response = await _client.GetActiveBlogCategoriesAsync(
            new CMSMicroservice.Protobuf.Protos.BlogCategory.GetActiveBlogCategoriesRequest());

        return response.Categories.Select(m => new BlogCategoryItemDto
        {
            Id = m.Id,
            Title = m.Title,
            Slug = m.Slug,
            Description = m.Description,
            IconName = m.IconName,
            SortOrder = m.SortOrder,
            IsActive = m.IsActive,
            PostCount = m.PostCount
        }).ToList();
    }

    public async Task<BlogCategoryItemDto?> GetByIdAsync(long id)
    {
        var response = await _client.GetBlogCategoryAsync(
            new CMSMicroservice.Protobuf.Protos.BlogCategory.GetBlogCategoryRequest { Id = id });
        if (response == null || response.Id <= 0) return null;

        return new BlogCategoryItemDto
        {
            Id = response.Id,
            Title = response.Title,
            Slug = response.Slug,
            Description = response.Description,
            IconName = response.IconName,
            SortOrder = response.SortOrder,
            IsActive = response.IsActive,
            PostCount = response.PostCount
        };
    }

    public async Task<long> CreateAsync(BlogCategoryEditDto dto)
    {
        var request = new CMSMicroservice.Protobuf.Protos.BlogCategory.CreateBlogCategoryRequest
        {
            Title = dto.Title,
            Slug = dto.Slug,
            Description = dto.Description ?? string.Empty,
            IconName = dto.IconName ?? string.Empty,
            SortOrder = dto.SortOrder,
            IsActive = dto.IsActive
        };

        var response = await _client.CreateBlogCategoryAsync(request);
        return response.Id;
    }

    public async Task UpdateAsync(long id, BlogCategoryEditDto dto)
    {
        var request = new CMSMicroservice.Protobuf.Protos.BlogCategory.UpdateBlogCategoryRequest
        {
            Id = id,
            Title = dto.Title,
            Slug = dto.Slug,
            Description = dto.Description ?? string.Empty,
            IconName = dto.IconName ?? string.Empty,
            SortOrder = dto.SortOrder,
            IsActive = dto.IsActive
        };

        await _client.UpdateBlogCategoryAsync(request);
    }

    public async Task DeleteAsync(long id)
    {
        await _client.DeleteBlogCategoryAsync(
            new CMSMicroservice.Protobuf.Protos.BlogCategory.DeleteBlogCategoryRequest { Id = id });
    }
}
