namespace BackOffice.Services.Content;

public class SitePageService : ISitePageService
{
    private readonly CMSMicroservice.Protobuf.Protos.SitePage.SitePageContract.SitePageContractClient _client;

    public SitePageService(
        CMSMicroservice.Protobuf.Protos.SitePage.SitePageContract.SitePageContractClient client)
    {
        _client = client;
    }

    public async Task<List<SitePageSummaryDto>> GetAllAsync()
    {
        var response = await _client.GetAllSitePagesAsync(
            new CMSMicroservice.Protobuf.Protos.SitePage.GetAllSitePagesRequest());

        return response.Pages.Select(m => new SitePageSummaryDto
        {
            Id = m.Id,
            PageKey = m.PageKey,
            Title = m.Title,
            IsActive = m.IsActive,
            SectionCount = m.SectionCount,
            LastModified = m.LastModified?.ToDateTime() ?? DateTime.MinValue
        }).ToList();
    }

    public async Task<SitePageDetailsDto?> GetByIdAsync(long id)
    {
        var response = await _client.GetSitePageAsync(
            new CMSMicroservice.Protobuf.Protos.SitePage.GetSitePageRequest { Id = id });

        if (response == null || response.Id <= 0) return null;

        var dto = new SitePageDetailsDto
        {
            Id = response.Id,
            PageKey = response.PageKey,
            Title = response.Title,
            MetaDescription = response.MetaDescription,
            HeroTitle = response.HeroTitle,
            HeroSubtitle = response.HeroSubtitle,
            HeroImagePath = response.HeroImagePath,
            IsActive = response.IsActive
        };

        foreach (var s in response.Sections)
        {
            dto.Sections.Add(new SitePageSectionItemDto
            {
                Id = s.Id,
                SectionKey = s.SectionKey,
                Title = s.Title,
                Subtitle = s.Subtitle,
                HtmlContent = s.HtmlContent,
                IconName = s.IconName,
                ImagePath = s.ImagePath,
                ImageThumbnailPath = s.ImageThumbnailPath,
                SortOrder = s.SortOrder,
                IsActive = s.IsActive,
                ExtraData = s.ExtraData
            });
        }

        return dto;
    }

    public async Task<long> CreateAsync(SitePageCreateDto dto)
    {
        var request = new CMSMicroservice.Protobuf.Protos.SitePage.CreateSitePageRequest
        {
            PageKey = dto.PageKey,
            Title = dto.Title,
            MetaDescription = dto.MetaDescription ?? string.Empty,
            HeroTitle = dto.HeroTitle ?? string.Empty,
            HeroSubtitle = dto.HeroSubtitle ?? string.Empty,
            IsActive = dto.IsActive
        };

        if (dto.ImageFile != null && !string.IsNullOrEmpty(dto.ImageMime) && !string.IsNullOrEmpty(dto.ImageFileName))
        {
            request.ImageFile = new CMSMicroservice.Protobuf.Protos.SitePage.SitePageImageFileModel
            {
                File = Google.Protobuf.ByteString.CopyFrom(dto.ImageFile),
                Mime = dto.ImageMime,
                FileName = dto.ImageFileName
            };
        }

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

    public async Task DeleteAsync(long id)
    {
        await _client.DeleteSitePageAsync(
            new CMSMicroservice.Protobuf.Protos.SitePage.DeleteSitePageRequest { Id = id });
    }

    public async Task UpdateAsync(long id, SitePageEditDto dto)
    {
        var request = new CMSMicroservice.Protobuf.Protos.SitePage.UpdateSitePageRequest
        {
            Id = id,
            Title = dto.Title,
            MetaDescription = dto.MetaDescription ?? string.Empty,
            HeroTitle = dto.HeroTitle ?? string.Empty,
            HeroSubtitle = dto.HeroSubtitle ?? string.Empty,
            HeroImagePath = dto.HeroImagePath ?? string.Empty,
            IsActive = dto.IsActive
        };

        if (dto.ImageFile != null && !string.IsNullOrEmpty(dto.ImageMime) && !string.IsNullOrEmpty(dto.ImageFileName))
        {
            request.ImageFile = new CMSMicroservice.Protobuf.Protos.SitePage.SitePageImageFileModel
            {
                File = Google.Protobuf.ByteString.CopyFrom(dto.ImageFile),
                Mime = dto.ImageMime,
                FileName = dto.ImageFileName
            };
        }

        await _client.UpdateSitePageAsync(request);
    }

    public async Task<long> CreateSectionAsync(SitePageSectionEditDto dto)
    {
        var request = new CMSMicroservice.Protobuf.Protos.SitePage.CreateSitePageSectionRequest
        {
            SitePageId = dto.SitePageId,
            SectionKey = dto.SectionKey,
            Title = dto.Title,
            Subtitle = dto.Subtitle ?? string.Empty,
            HtmlContent = dto.HtmlContent ?? string.Empty,
            IconName = dto.IconName ?? string.Empty,
            ImagePath = dto.ImagePath ?? string.Empty,
            ImageThumbnailPath = dto.ImageThumbnailPath ?? string.Empty,
            SortOrder = dto.SortOrder,
            ExtraData = dto.ExtraData ?? string.Empty
        };

        if (dto.ImageFile != null && !string.IsNullOrEmpty(dto.ImageMime) && !string.IsNullOrEmpty(dto.ImageFileName))
        {
            request.ImageFile = new CMSMicroservice.Protobuf.Protos.SitePage.SitePageImageFileModel
            {
                File = Google.Protobuf.ByteString.CopyFrom(dto.ImageFile),
                Mime = dto.ImageMime,
                FileName = dto.ImageFileName
            };
        }

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

    public async Task UpdateSectionAsync(long id, SitePageSectionEditDto dto)
    {
        var request = new CMSMicroservice.Protobuf.Protos.SitePage.UpdateSitePageSectionRequest
        {
            Id = id,
            SectionKey = dto.SectionKey,
            Title = dto.Title,
            Subtitle = dto.Subtitle ?? string.Empty,
            HtmlContent = dto.HtmlContent ?? string.Empty,
            IconName = dto.IconName ?? string.Empty,
            ImagePath = dto.ImagePath ?? string.Empty,
            ImageThumbnailPath = dto.ImageThumbnailPath ?? string.Empty,
            SortOrder = dto.SortOrder,
            IsActive = dto.IsActive,
            ExtraData = dto.ExtraData ?? string.Empty
        };

        if (dto.ImageFile != null && !string.IsNullOrEmpty(dto.ImageMime) && !string.IsNullOrEmpty(dto.ImageFileName))
        {
            request.ImageFile = new CMSMicroservice.Protobuf.Protos.SitePage.SitePageImageFileModel
            {
                File = Google.Protobuf.ByteString.CopyFrom(dto.ImageFile),
                Mime = dto.ImageMime,
                FileName = dto.ImageFileName
            };
        }

        await _client.UpdateSitePageSectionAsync(request);
    }

    public async Task DeleteSectionAsync(long id)
    {
        await _client.DeleteSitePageSectionAsync(
            new CMSMicroservice.Protobuf.Protos.SitePage.DeleteSitePageSectionRequest { Id = id });
    }

    public async Task ReorderSectionsAsync(List<SectionSortItemDto> items)
    {
        var request = new CMSMicroservice.Protobuf.Protos.SitePage.ReorderSitePageSectionsRequest();

        foreach (var item in items)
        {
            request.Items.Add(new CMSMicroservice.Protobuf.Protos.SitePage.SectionSortItem
            {
                Id = item.Id,
                SortOrder = item.SortOrder
            });
        }

        await _client.ReorderSitePageSectionsAsync(request);
    }
}
