using System.Security.Cryptography;
using System.Text;

namespace GerbilManagerWebAPI.Import.Rpro3
{
    /// <summary>
    /// Deterministische GUIDs aus stabilen RPRO3-Schlüsseln (analog zum Python `generate_guid`-Muster).
    /// Gleicher Schlüssel → gleiche GUID über Re-Imports hinweg → idempotent.
    /// MD5-basiert (Namespace-Präfix vermeidet Kollisionen mit anderen Entitätstypen).
    /// </summary>
    public static class Rpro3Guid
    {
        public const string ImportSource = "RennmausPro III";

        public static Guid For(string @namespace, string key)
        {
            var bytes = MD5.HashData(Encoding.UTF8.GetBytes($"rpro3:{@namespace}:{key}"));
            return new Guid(bytes);
        }

        public static Guid Gerbil(string clusterRootRid) => For("gerbil", clusterRootRid);
        public static Guid Litter(int wurfId) => For("litter", wurfId.ToString());
        public static Guid Contact(string ns, int id) => For("contact", $"{ns}:{id}");
        public static Guid Health(string tid, string disc, int seq) => For("health", $"{tid}:{seq}:{disc}");
        public static Guid Weight(string tid, int seq) => For("weight", $"{tid}:{seq}");

        // Feature-Tabellen — deterministische IDs für Idempotenz (Re-Import ersetzt dieselben Zeilen).
        public static Guid Enclosure(int beckenId) => For("enclosure", beckenId.ToString());
        public static Guid Acquisition(int herktierId) => For("acquisition", herktierId.ToString());
        public static Guid Reservation(string tid) => For("reservation", tid);
        public static Guid Return(int getbackId) => For("return", getbackId.ToString());
        public static Guid WaitingList(int nachfrageId) => For("waitinglist", nachfrageId.ToString());
        public static Guid Exhibition(int auszId) => For("exhibition", auszId.ToString());

        /// <summary>Stabiler ExternalRef-Wert (Gerbil/Litter Idempotenzschlüssel, UNIQUE-Spalte).</summary>
        public static string GerbilRef(string rootRid) => $"rpro3:{rootRid}";
        public static string LitterRef(int wurfId) => $"rpro3-wurf:{wurfId}";
    }
}
