using System.Net.Http.Json;
using Microsoft.Extensions.Logging;
using LittleShop.Client.Models;

namespace LittleShop.Client.Services;

public class CustomerService : ICustomerService
{
    private readonly HttpClient _httpClient;
    private readonly ILogger<CustomerService> _logger;

    public CustomerService(HttpClient httpClient, ILogger<CustomerService> logger)
    {
        _httpClient = httpClient;
        _logger = logger;
    }

    public async Task<ApiResponse<Customer>> GetCustomerByIdAsync(Guid id)
    {
        try
        {
            var response = await _httpClient.GetAsync($"api/customers/{id}");
            
            if (response.IsSuccessStatusCode)
            {
                var customer = await response.Content.ReadFromJsonAsync<Customer>();
                return ApiResponse<Customer>.Success(customer!);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<Customer>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get customer {CustomerId}", id);
            return ApiResponse<Customer>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }

    public async Task<ApiResponse<Customer>> GetCustomerByTelegramUserIdAsync(long telegramUserId)
    {
        try
        {
            var response = await _httpClient.GetAsync($"api/customers/by-telegram/{telegramUserId}");
            
            if (response.IsSuccessStatusCode)
            {
                var customer = await response.Content.ReadFromJsonAsync<Customer>();
                return ApiResponse<Customer>.Success(customer!);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<Customer>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get customer by Telegram ID {TelegramUserId}", telegramUserId);
            return ApiResponse<Customer>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }

    public async Task<ApiResponse<Customer>> CreateCustomerAsync(CreateCustomerRequest request)
    {
        try
        {
            var response = await _httpClient.PostAsJsonAsync("api/customers", request);
            
            if (response.IsSuccessStatusCode)
            {
                var customer = await response.Content.ReadFromJsonAsync<Customer>();
                return ApiResponse<Customer>.Success(customer!);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<Customer>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to create customer");
            return ApiResponse<Customer>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }

    public async Task<ApiResponse<Customer>> GetOrCreateCustomerAsync(CreateCustomerRequest request)
    {
        try
        {
            var response = await _httpClient.PostAsJsonAsync("api/customers/get-or-create", request);
            
            if (response.IsSuccessStatusCode)
            {
                var customer = await response.Content.ReadFromJsonAsync<Customer>();
                return ApiResponse<Customer>.Success(customer!);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<Customer>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get or create customer");
            return ApiResponse<Customer>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }

    public async Task<ApiResponse<Customer>> UpdateCustomerAsync(Guid id, UpdateCustomerRequest request)
    {
        try
        {
            var response = await _httpClient.PutAsJsonAsync($"api/customers/{id}", request);
            
            if (response.IsSuccessStatusCode)
            {
                var customer = await response.Content.ReadFromJsonAsync<Customer>();
                return ApiResponse<Customer>.Success(customer!);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<Customer>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to update customer {CustomerId}", id);
            return ApiResponse<Customer>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }

    public async Task<ApiResponse<bool>> BlockCustomerAsync(Guid id, string reason)
    {
        try
        {
            var response = await _httpClient.PostAsJsonAsync($"api/customers/{id}/block", reason);
            
            if (response.IsSuccessStatusCode)
            {
                return ApiResponse<bool>.Success(true);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<bool>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to block customer {CustomerId}", id);
            return ApiResponse<bool>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }

    public async Task<ApiResponse<bool>> UnblockCustomerAsync(Guid id)
    {
        try
        {
            var response = await _httpClient.PostAsync($"api/customers/{id}/unblock", null);
            
            if (response.IsSuccessStatusCode)
            {
                return ApiResponse<bool>.Success(true);
            }
            
            var error = await response.Content.ReadAsStringAsync();
            return ApiResponse<bool>.Failure(error, response.StatusCode);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to unblock customer {CustomerId}", id);
            return ApiResponse<bool>.Failure(
                ex.Message, 
                System.Net.HttpStatusCode.InternalServerError);
        }
    }
}