using UnityEngine;

namespace Davanci.Nexus.Editor
{
    // One-call IAP setup from Nexus Setup: Unity Services toggle, UPM install, define, tangles.
    public static class NexusIapPipeline
    {
        public static void SyncFromConfig(NexusConfig cfg)
        {
            if (cfg == null) return;

            NexusUnityPurchasingServices.SetEnabled(cfg.enableIap);
            if (!cfg.enableIap) return;

            if (!NexusIapInstaller.Installed)
            {
                NexusIapInstaller.Install();
                return;
            }

            NexusIapInstaller.SetDefine(true);
            NexusIapTangleSetup.GenerateAll(cfg);
            NexusIapValidationInstaller.Sync();
        }

        // Called after async UPM install completes.
        internal static void OnPackageInstalled()
        {
            var cfg = NexusConfig.Load();
            if (cfg == null || !cfg.enableIap) return;
            NexusUnityPurchasingServices.SetEnabled(true);
            NexusIapTangleSetup.GenerateAll(cfg);
            NexusIapValidationInstaller.Sync();
        }
    }
}
