using System.Reflection.Metadata.Ecma335;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Web;
using Bimix.Domain.Entities;
using Bimix.Infrastructure.Data;
using Microsoft.Extensions.Configuration;
namespace Bimix.Infrastructure.Sync;
public class ProductSyncService(HttpClient httpClient, BimixDbContext db, IConfiguration configuration)
{
///
/// Dekoduje encje HTML w ciągu znaków (np. " na ")
///
private string DecodeHtmlEntities(string text)
{
if (string.IsNullOrEmpty(text))
return text;
return HttpUtility.HtmlDecode(text);
}
public async Task RunAsync()
{
var apiKey = configuration["E5_CRM:ApiKey"];
var syncState = db.SyncStates.FirstOrDefault(x => x.Entity == "Product") ?? new SyncState { Entity = "Product", LastSynced = 0};
var url = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.list&since={syncState.LastSynced}";
var response = await httpClient.GetStringAsync(url);
var products = JsonSerializer.Deserialize>(response);
if (products == null) return;
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
foreach (var p in products)
{
var idStr = p.GetProperty("id").GetString() ?? "";
var name = DecodeHtmlEntities(p.GetProperty("name").GetString() ?? "");
var code = DecodeHtmlEntities(p.GetProperty("code").GetString() ?? "");
var stockAddresses = DecodeHtmlEntities(p.GetProperty("stock_addresses").GetString() ?? "");
var ean = DecodeHtmlEntities(p.GetProperty("ean").GetString() ?? "");
if (!Guid.TryParse(idStr, out Guid id))
{
Console.WriteLine($"[SYNC] Skipping product with wrong ID: '{idStr}', Name: '{name}'");
continue;
}
var existing = db.Products.FirstOrDefault(x => x.Id == id);
if (existing == null)
{
var product = new Product
{
Id = id,
Name = name,
Ean = ean,
Code = code,
StockAddresses = stockAddresses,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
db.Products.Add(product);
}
else
{
existing.Name = name;
existing.UpdatedAt = DateTime.UtcNow;
}
Console.WriteLine($"[SYNC] Updated product ID: '{idStr}', Name: '{name}'");
var exportedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var updateUrl = $"https://crm.e5.pl/REST/index.php?key={apiKey}&action=export.products.setExportedAt&id={id}&exportedAt={exportedAt}";
await httpClient.GetAsync(updateUrl);
}
syncState.LastSynced = now;
if (db.SyncStates.FirstOrDefault(x => x.Entity == "Product") == null)
{
db.SyncStates.Add(syncState);
}
else
{
db.SyncStates.Update(syncState);
}
await db.SaveChangesAsync();
}
}