using System.Globalization;
using Jellyfin.Plugin.JRay.Models;
namespace Jellyfin.Plugin.JRay.Services;
///
/// The one place that decides whether a truth file speaks a version this plugin
/// understands.
///
///
/// Flag day, not dual-accept. Only is
/// accepted; every other value is refused on every path — sidecar read, managed
/// PUT, managed store load, and converted manifest. There is no
/// transitional v1 read path.
///
/// All three components are pre-release and move together, and the alternative
/// carries a cost that outlasts the transition: a v1 read path is the one nobody
/// exercises, so it is the one that rots, and it would have to be dragged
/// through every subsequent change to the reader.
///
///
/// The consequence is stated rather than discovered: v1 sidecar files
/// already on disk stop being read at the bump and stay dark until the library
/// is re-extracted. Callers log the rejection naming the file and the version
/// found, because an item that looks un-extracted when it is merely stale is the
/// failure mode that wastes a user's compute.
///
///
/// This exists as a shared unit because the check used to live only in the
/// PUT controller while sidecar reads did not check at all — so the
/// format the plugin claimed to require and the format it would actually parse
/// were different things.
///
///
// TRACES: JR-003 | SR-003
public static class TruthSchema
{
///
/// The only schema_version this plugin reads or writes.
///
///
/// System-level (SR-003): the same number means the same format in all three
/// repos, and is incremented once per breaking change across all of them.
///
public const int SupportedVersion = 2;
///
/// Determines whether a truth file speaks the supported version.
///
/// The parsed truth file, which may be null.
/// true only when the version matches exactly.
public static bool IsSupported(TruthFile? truth)
=> truth is not null && truth.SchemaVersion == SupportedVersion;
///
/// Builds the message describing why a truth file was refused.
///
///
/// Names both the version found and the version expected. A rejection that
/// says only "unsupported" leaves the reader unable to tell a stale file
/// from a corrupt one.
///
/// The version encountered.
/// A message naming both versions.
public static string DescribeRejection(int found)
=> string.Create(
CultureInfo.InvariantCulture,
$"Unsupported schema_version {found}; expected {SupportedVersion}. Re-extract the item — v1 truth data is no longer read.");
}