#if DAVANCI_LEVELPLAY
using Unity.Services.LevelPlay;
using UnityEngine;

namespace Davanci.Nexus
{
    // LevelPlay privacy flags set before Init.
    //
    // CMP (InMobi Choice) is separate: it writes IAB TCF/GPP strings that LevelPlay/networks
    // read automatically. Do NOT map ATT → SetGDPRConsent when CMP is on — that would override
    // the user's GDPR choice with an IDFA permission that is not GDPR consent.
    //
    // When CMP is off: ATT still drives LevelPlay personalized-ads / CCPA flags (US / fill).
    static class NexusLevelPlayPrivacy
    {
        public static void ApplyBeforeInit(bool? attTrackingAuthorized)
        {
            var cfg = NexusConfig.Load();
            if (cfg != null && cfg.enableCmp)
            {
                // GDPR/US privacy already handled by the CMP → IAB strings. Leave LevelPlay alone.
                Debug.Log("[Nexus] LevelPlay privacy: CMP enabled — using IAB TCF/GPP from InMobi Choice.");
                return;
            }

            if (attTrackingAuthorized == null) return;

            bool allowPersonalized = attTrackingAuthorized.Value;
            LevelPlayPrivacySettings.SetGDPRConsent(allowPersonalized);
            LevelPlayPrivacySettings.SetCCPA(!allowPersonalized);
            LevelPlay.SetMetaData("do_not_sell", allowPersonalized ? "false" : "true");

            Debug.Log(allowPersonalized
                ? "[Nexus] LevelPlay privacy: personalized ads allowed (ATT authorized)."
                : "[Nexus] LevelPlay privacy: limited ads (ATT denied/restricted).");
        }
    }
}
#endif
