namespace TaxBaik.Web.Services;

using System.Net.Http.Json;
using TaxBaik.Application.DTOs;

public interface IBlogBrowserClient
{
    Task<(IEnumerable<BlogPostResponseDto> Items, int Total)> GetAdminPagedAsync(int page = 1, int pageSize = 20, CancellationToken ct = default);
    Task<(IEnumerable<BlogPostResponseDto> Items, int Total)> GetArchivedPagedAsync(int page = 1, int pageSize = 20, CancellationToken ct = default);
    Task<BlogPostResponseDto?> GetByIdAsync(int id, CancellationToken ct = default);
    Task<BlogPostResponseDto?> CreateAsync(CreateBlogPostDto dto, CancellationToken ct = default);
    Task<BlogPostResponseDto?> UpdateAsync(int id, CreateBlogPostDto dto, CancellationToken ct = default);
    Task<bool> DeleteAsync(int id, CancellationToken ct = default);
    Task<bool> RestoreAsync(int id, CancellationToken ct = default);
    Task<bool> TogglePublishAsync(int id, CreateBlogPostDto dto, CancellationToken ct = default);
}

public class BlogBrowserClient : IBlogBrowserClient
{
    private readonly HttpClient _http;
    private readonly ILogger<BlogBrowserClient> _logger;
    private readonly ITokenStore _tokenStore;

    public BlogBrowserClient(HttpClient http, ILogger<BlogBrowserClient> logger, ITokenStore tokenStore)
    {
        _http = http;
        _logger = logger;
        _tokenStore = tokenStore;
    }

    private void EnsureAuthHeader()
    {
        if (!string.IsNullOrEmpty(_tokenStore.AccessToken))
            _http.DefaultRequestHeaders.Authorization = new("Bearer", _tokenStore.AccessToken);
        else
            _http.DefaultRequestHeaders.Authorization = null;
    }

    public async Task<(IEnumerable<BlogPostResponseDto> Items, int Total)> GetAdminPagedAsync(int page = 1, int pageSize = 20, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        var result = await _http.GetFromJsonAsync<PagedResponse>($"blog/admin?page={page}&pageSize={pageSize}", ct);
        return result != null ? (result.Data, result.Total) : ([], 0);
    }

    public async Task<(IEnumerable<BlogPostResponseDto> Items, int Total)> GetArchivedPagedAsync(int page = 1, int pageSize = 20, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        var result = await _http.GetFromJsonAsync<PagedResponse>($"blog/admin/archived?page={page}&pageSize={pageSize}", ct);
        return result != null ? (result.Data, result.Total) : ([], 0);
    }

    public async Task<BlogPostResponseDto?> GetByIdAsync(int id, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        return await _http.GetFromJsonAsync<BlogPostResponseDto>($"blog/{id}", ct);
    }

    public async Task<BlogPostResponseDto?> CreateAsync(CreateBlogPostDto dto, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        var response = await _http.PostAsJsonAsync("blog", dto, ct);
        if (!response.IsSuccessStatusCode)
            return null;
        var content = await response.Content.ReadAsStringAsync(ct);
        return System.Text.Json.JsonSerializer.Deserialize<BlogPostResponseDto>(content, new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
    }

    public async Task<BlogPostResponseDto?> UpdateAsync(int id, CreateBlogPostDto dto, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        var response = await _http.PutAsJsonAsync($"blog/{id}", dto, ct);
        if (!response.IsSuccessStatusCode)
            return null;
        var content = await response.Content.ReadAsStringAsync(ct);
        return System.Text.Json.JsonSerializer.Deserialize<BlogPostResponseDto>(content, new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
    }

    public async Task<bool> DeleteAsync(int id, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        var response = await _http.DeleteAsync($"blog/{id}", ct);
        return response.IsSuccessStatusCode;
    }

    public async Task<bool> RestoreAsync(int id, CancellationToken ct = default)
    {
        EnsureAuthHeader();
        var response = await _http.PostAsync($"blog/{id}/restore", null, ct);
        return response.IsSuccessStatusCode;
    }

    public async Task<bool> TogglePublishAsync(int id, CreateBlogPostDto dto, CancellationToken ct = default)
    {
        var result = await UpdateAsync(id, dto, ct);
        return result != null;
    }

    private sealed class PagedResponse
    {
        public List<BlogPostResponseDto> Data { get; set; } = [];
        public int Total { get; set; }
    }
}
