using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SlpModularCms.Core.Media.Library;
using SlpModularCms.Modules.Media.Data;
using SlpModularCms.Modules.Media.Data.Entities;
namespace SlpModularCms.Modules.Media.Services;
/// What a copy did and what the verification found.
public sealed record MediaCopyReport(int Assets, int Uses, IReadOnlyList Differences)
{
public bool Verified => Differences.Count == 0;
}
public sealed class MediaCopyVerificationException(MediaCopyReport report)
: Exception($"The copy of the media library does not match its source: {string.Join("; ", report.Differences.Take(5))}")
{
public MediaCopyReport Report { get; } = report;
}
///
/// Copies the photos and uses of every module () into the library's own tables and
/// verifies the copy against the source (migration plan, decision 13). The copy MIRRORS the source: re-running it gives
/// the same result, rows of a module that no longer exist at the source are removed from the copy. The modules' own data
/// is never changed. A difference after copying rolls the whole copy back and raises .
///
public sealed class MediaLibraryCopier(MediaDbContext db, IEnumerable sources, ILogger logger)
{
private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web);
/// Mirror only this module (the write-through); null = every module (the verified copy).
public async Task CopyAsync(CancellationToken cancellationToken, string? onlyModule = null)
{
var assets = new List();
var uses = new List();
var modules = new List();
foreach (var source in sources.Where(s => onlyModule is null || s.Module == onlyModule))
{
modules.Add(source.Module);
assets.AddRange(await source.ListAssetsAsync(cancellationToken));
uses.AddRange(await source.ListUsesAsync(cancellationToken));
}
var transaction = db.Database.IsRelational() ? await db.Database.BeginTransactionAsync(cancellationToken) : null;
try
{
await MirrorAsync(assets, uses, modules, cancellationToken);
await db.SaveChangesAsync(cancellationToken);
var report = await VerifyAsync(assets, uses, modules, cancellationToken);
if (!report.Verified)
{
throw new MediaCopyVerificationException(report);
}
if (transaction is not null)
{
await transaction.CommitAsync(cancellationToken);
}
logger.LogInformation("Media library copy verified: {Assets} photos and {Uses} uses from {Modules}.", report.Assets, report.Uses, string.Join(", ", modules));
return report;
}
catch
{
if (transaction is not null)
{
await transaction.RollbackAsync(CancellationToken.None);
}
db.ChangeTracker.Clear();
throw;
}
finally
{
if (transaction is not null)
{
await transaction.DisposeAsync();
}
}
}
private async Task MirrorAsync(IReadOnlyList assets, IReadOnlyList uses, IReadOnlyList modules, CancellationToken cancellationToken)
{
var existingAssets = new Dictionary();
var existingUses = new Dictionary();
foreach (var module in modules)
{
// One equality query per module: the MySQL provider cannot translate a list parameter in an IN clause.
foreach (var asset in await db.MediaAssets.Where(a => a.Module == module).ToListAsync(cancellationToken))
{
existingAssets[asset.Id] = asset;
}
foreach (var use in await db.MediaUses.Where(u => u.Module == module).ToListAsync(cancellationToken))
{
existingUses[use.Id] = use;
}
}
foreach (var snapshot in assets)
{
if (!existingAssets.Remove(snapshot.Id, out var row))
{
row = new MediaAsset { Id = snapshot.Id };
db.MediaAssets.Add(row);
}
row.Module = snapshot.Module;
row.OriginalSha256 = snapshot.OriginalSha256;
row.OriginalKey = snapshot.OriginalKey;
row.SourceFormat = snapshot.SourceFormat;
row.Width = snapshot.Width;
row.Height = snapshot.Height;
row.Bytes = snapshot.Bytes;
row.DefaultFocusX = snapshot.FocusX;
row.DefaultFocusY = snapshot.FocusY;
row.DefaultZoom = snapshot.Zoom;
row.CreatedAt = snapshot.CreatedAt;
row.CreatedByUserId = snapshot.CreatedByUserId;
row.OriginalFileName = snapshot.OriginalFileName;
row.LastUsedAt = snapshot.LastUsedAt;
row.DeletedAt = snapshot.DeletedAt;
row.SetHash = snapshot.SetHash;
row.Variants = snapshot.VariantsJson;
(row.WideFocusX, row.WideFocusY, row.WideZoom) = (snapshot.WideFocusX, snapshot.WideFocusY, snapshot.WideZoom);
(row.TallFocusX, row.TallFocusY, row.TallZoom) = (snapshot.TallFocusX, snapshot.TallFocusY, snapshot.TallZoom);
}
// Uses reference their asset; the crops of a use are the crops of its photo (the hero's wide and tall).
var cropsByAsset = assets.ToDictionary(a => a.Id, a => JsonSerializer.Serialize(a.Crops, Json));
foreach (var snapshot in uses)
{
if (!existingUses.Remove(snapshot.Id, out var row))
{
row = new MediaAssetUse { Id = snapshot.Id };
db.MediaUses.Add(row);
}
row.AssetId = snapshot.AssetId;
row.Module = snapshot.Module;
row.PlaceKind = snapshot.PlaceKind;
row.Position = snapshot.Position;
row.Active = snapshot.Active;
row.SetHash = snapshot.SetHash;
row.Variants = snapshot.VariantsJson;
row.Crops = snapshot.Crops is not null ? JsonSerializer.Serialize(snapshot.Crops, Json) : cropsByAsset.TryGetValue(snapshot.AssetId, out var crops) ? crops : "[]";
}
// What is left no longer exists at the source.
db.MediaUses.RemoveRange(existingUses.Values);
db.MediaAssets.RemoveRange(existingAssets.Values);
}
private async Task VerifyAsync(IReadOnlyList assets, IReadOnlyList uses, IReadOnlyList modules, CancellationToken cancellationToken)
{
var differences = new List();
var copiedAssets = new Dictionary();
var copiedUses = new Dictionary();
foreach (var module in modules)
{
foreach (var asset in await db.MediaAssets.AsNoTracking().Where(a => a.Module == module).ToListAsync(cancellationToken))
{
copiedAssets[asset.Id] = asset;
}
foreach (var use in await db.MediaUses.AsNoTracking().Where(u => u.Module == module).ToListAsync(cancellationToken))
{
copiedUses[use.Id] = use;
}
}
if (copiedAssets.Count != assets.Count)
{
differences.Add($"photos: source {assets.Count}, copy {copiedAssets.Count}");
}
if (copiedUses.Count != uses.Count)
{
differences.Add($"uses: source {uses.Count}, copy {copiedUses.Count}");
}
foreach (var asset in assets)
{
if (!copiedAssets.TryGetValue(asset.Id, out var copy))
{
differences.Add($"photo {asset.Id} is missing in the copy");
}
else if (copy.OriginalSha256 != asset.OriginalSha256 || copy.OriginalKey != asset.OriginalKey || copy.Bytes != asset.Bytes)
{
differences.Add($"photo {asset.Id}: hash, key or size differs");
}
else if (copy.SetHash != asset.SetHash || copy.Variants != asset.VariantsJson)
{
differences.Add($"photo {asset.Id}: set hash or variants differ");
}
}
foreach (var use in uses)
{
if (!copiedUses.TryGetValue(use.Id, out var copy))
{
differences.Add($"use {use.Id} is missing in the copy");
}
else if (copy.SetHash != use.SetHash || copy.AssetId != use.AssetId || copy.Variants != use.VariantsJson)
{
differences.Add($"use {use.Id}: set hash, variants or photo differs");
}
}
return new MediaCopyReport(copiedAssets.Count, copiedUses.Count, differences);
}
}