using CMSMicroservice.Protobuf.Protos.SitePageSettings;

namespace BackOffice.Services.Content;

public class SitePageSettingsService : ISitePageSettingsService
{
    private readonly SitePageSettingsContract.SitePageSettingsContractClient _client;

    public SitePageSettingsService(SitePageSettingsContract.SitePageSettingsContractClient client)
    {
        _client = client;
    }

    public async Task<List<PageSettingsSummaryDto>> GetAllAsync()
    {
        var response = await _client.GetAllPageSettingsAsync(new GetAllPageSettingsRequest());

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

    public async Task<PageSettingsDetailsDto?> GetByKeyAsync(string pageKey)
    {
        var response = await _client.GetPageSettingsAsync(
            new GetPageSettingsRequest { PageKey = pageKey });

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

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

        foreach (var img in response.Images)
        {
            dto.Images.Add(new PageImageItemDto
            {
                Id = img.Id,
                ImageGroup = img.ImageGroup,
                Title = img.Title,
                Subtitle = img.Subtitle,
                Description = img.Description,
                ImagePath = img.ImagePath,
                ThumbnailPath = img.ThumbnailPath,
                LinkUrl = img.LinkUrl,
                IconName = img.IconName,
                SortOrder = img.SortOrder,
                IsActive = img.IsActive
            });
        }

        return dto;
    }

    public async Task<SaveResultDto> SaveSettingsAsync(SavePageSettingsDto dto)
    {
        var request = new SavePageSettingsRequest
        {
            PageKey = dto.PageKey,
            Title = dto.Title,
            MetaDescription = dto.MetaDescription ?? string.Empty,
            HeroTitle = dto.HeroTitle ?? string.Empty,
            HeroSubtitle = dto.HeroSubtitle ?? string.Empty,
            IsActive = dto.IsActive,
            SettingsJson = dto.SettingsJson ?? string.Empty
        };

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

        var response = await _client.SavePageSettingsAsync(request);
        return new SaveResultDto { Id = response.Id, Success = response.Success };
    }

    public async Task<SaveResultDto> SaveImageAsync(SavePageImageDto dto)
    {
        var request = new SavePageImageRequest
        {
            Id = dto.Id,
            SitePageSettingsId = dto.SitePageSettingsId,
            ImageGroup = dto.ImageGroup,
            Title = dto.Title ?? string.Empty,
            Subtitle = dto.Subtitle ?? string.Empty,
            Description = dto.Description ?? string.Empty,
            LinkUrl = dto.LinkUrl ?? string.Empty,
            IconName = dto.IconName ?? string.Empty,
            SortOrder = dto.SortOrder,
            IsActive = dto.IsActive
        };

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

        var response = await _client.SavePageImageAsync(request);
        return new SaveResultDto { Id = response.Id, Success = response.Success };
    }

    public async Task DeleteImageAsync(long id)
    {
        await _client.DeletePageImageAsync(new DeletePageImageRequest { Id = id });
    }
}
