using System; using System.Threading.Tasks; using ComplianceNFs.Core.Entities; using ComplianceNFs.Core.Ports; using Npgsql; using Newtonsoft.Json; using System.Numerics; using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; namespace ComplianceNFs.Infrastructure.Repositories { public class AttachmentRepository(ComplianceNFsDbContext dbContext, ILogger logger) : IAttachmentRepository { private readonly ComplianceNFsDbContext _dbContext = dbContext; private readonly ILogger _logger = logger; public async Task SaveRawAsync(EnergyInvoice invoice) { try { await _dbContext.EnergyInvoices.AddAsync(invoice); await _dbContext.SaveChangesAsync(); _logger.LogInformation("Saved raw invoice {InvoiceId} to EnergyInvoices table.", invoice.InvoiceId); } catch (Exception ex) { _logger.LogError(ex, "Error saving raw invoice {InvoiceId} to EnergyInvoices table.", invoice.InvoiceId); throw; } } public async Task UpdateMatchAsync(int invoiceId, BigInteger matchedCodTE, InvoiceStatus status, string notes) { try { var invoice = await _dbContext.EnergyInvoices.FirstOrDefaultAsync(e => e.InvoiceId == invoiceId) ?? throw new InvalidOperationException($"Invoice with ID {invoiceId} not found."); invoice.MatchedCodTE = matchedCodTE; invoice.Status = status; invoice.DiscrepancyNotes = notes; await _dbContext.SaveChangesAsync(); _logger.LogInformation("Updated match for invoice {InvoiceId}.", invoiceId); } catch (Exception ex) { _logger.LogError(ex, "Error updating match for invoice {InvoiceId}.", invoiceId); throw; } } } }