using System.Collections.Generic;
using UnityEngine;

namespace Davanci.Nexus
{
    // Which ad mediation SDK serves ads at runtime.
    public enum NexusMediation { AppLovinMax, LevelPlay }

    // Central configuration, stored in Resources and loaded at runtime.
    // Edit via Davanci Nexus → Nexus Setup, or by selecting the asset directly.
    public class NexusConfig : ScriptableObject
    {
        public const string ResourcePath = "NexusConfig";

        [Header("Permissions")]
        public bool requestATT = true;
        public bool requestNotifications = true;

        [Header("Splash Screen")]
        [Tooltip("Optional logo override on the splash Image. Leave empty to use the logo set on the scene.")]
        public Sprite splashLogo;
        [Tooltip("Minimum seconds to show the splash on return visits (after the first permissions flow).")]
        public float splashReturnMinSeconds = 1.5f;

        [Header("Privacy / CMP (InMobi Choice)")]
        [Tooltip("Show a certified CMP consent UI (InMobi Choice) at startup for GDPR (EEA) + US privacy. Requires the InMobi CMP SDK in Assets/Plugins and DAVANCI_INMOBI_CMP.")]
        public bool enableCmp = false;
        [Tooltip("InMobi Choice property code (pcode), e.g. eEf1M9DDE84By.")]
        public string inmobiCmpPcode = "";

        [Header("Retention Notifications (D1 / D3 / D7)")]
        [Tooltip("Schedule local come-back reminders on day 1, 3 and 7 after the player's first session. Requires notification permission and com.unity.mobile.notifications (DAVANCI_NOTIFICATIONS).")]
        public bool enableRetentionNotifications = true;
        [Tooltip("Local hour (0–23) on each retention day when the notification is delivered.")]
        public int retentionSendHourLocal = 19;
        public bool retentionD1Enabled = true;
        public string retentionD1Title = "Come back!";
        public string retentionD1Body = "Your daily reward is waiting.";
        public bool retentionD3Enabled = true;
        public string retentionD3Title = "We miss you!";
        public string retentionD3Body = "Jump back in — something new is ready for you.";
        public bool retentionD7Enabled = true;
        public string retentionD7Title = "One week in!";
        public string retentionD7Body = "Claim your bonus and keep the streak going.";

        [Header("AppsFlyer")]
        public string appsFlyerDevKey = "";
        public string appsFlyerAppID = "";
        [Tooltip("Also mirror every Custom()/design event to AppsFlyer as a rich event. IAP and ad revenue always go to AppsFlyer regardless. Leave off to avoid AppsFlyer's event volume limits.")]
        public bool appsFlyerMirrorEvents = false;

        [Header("Store / Reviews")]
        [Tooltip("Numeric App Store ID (e.g. 6777495705), used to open the iOS review page from a manual 'Rate us' button. The native in-app sheet doesn't need it.")]
        public string iosAppStoreId = "";
        [Tooltip("Android package name for the Play store link. Leave empty to use the app's own identifier.")]
        public string androidPackageName = "";

        [Header("Feedback")]
        [Tooltip("Firebase Realtime Database URL (e.g. https://davancigames-default-rtdb.firebaseio.com/). NexusFeedback.Submit() POSTs unhappy-player feedback to <url>/feedback.json, no extra SDK needed.")]
        public string realtimeDbUrl = "https://davancigames-default-rtdb.firebaseio.com/";

        [Header("Remote Config / A-B Testing")]
        [Tooltip("Master switch. When off, NexusRemoteConfig.Get* always returns the default you pass in and no network call is made.")]
        public bool enableRemoteConfig = false;
        [Tooltip("Your game's ID on the DavanciNexus platform. Copy it from the dashboard — it selects which config this build receives.")]
        public string gameId = "";
        [Tooltip("Seconds to wait for the config fetch before giving up and using cached/default values. The fetch never blocks the game.")]
        public int remoteConfigTimeoutSeconds = 5;

        [Header("GameAnalytics (iOS)")]
        public string gaIosGameKey = "";
        public string gaIosSecretKey = "";

        [Header("GameAnalytics (Android)")]
        public string gaAndroidGameKey = "";
        public string gaAndroidSecretKey = "";

        [Header("Cloud Save")]
        [Tooltip("Master switch. When off, NexusCloudSave uses the local-only fallback and no iCloud entitlement is added to iOS builds, so games that don't opt in are unaffected.")]
        public bool enableCloudSave = false;
        [Tooltip("Spawn NexusCloudSaveRunner at startup so the save loads on launch and is written on pause/quit automatically. Your game supplies the bytes via NexusCloudSaveRunner.CollectSave / ApplyLoad.")]
        public bool cloudSaveAutoSync = false;
        [Tooltip("Android only: the Google Play Games Saved Games snapshot name the player's save is stored under. Stable, one per game. Ignored on iOS (which keys off the bundle ID).")]
        public string gpgsSnapshotName = "nexus_save";
        [Tooltip("Android Play Games setup: OAuth 2.0 Web Client ID from the Play Console (optional but recommended for Saved Games sign-in).")]
        public string gpgsWebClientId = "";
        [Tooltip("Android Play Games setup: paste the Android Resources XML from the Play Console Setup & management page.")]
        [TextArea(3, 12)]
        public string gpgsResourcesXml = "";

        [Header("In-App Purchases")]
        [Tooltip("Master switch. When off, NexusIAP stays uninitialized and no store calls are made. Requires the Unity IAP package (DAVANCI_IAP).")]
        public bool enableIap = false;
        [Tooltip("Initialize IAP automatically at startup (recommended). Off if you'd rather call DavanciNexus.InitializeIAP() yourself.")]
        public bool iapAutoInitialize = true;
        [Tooltip("In the Unity Editor, use the real Unity IAP store instead of the built-in fake. Off by default — real billing in Editor is unreliable; test on device for store builds.")]
        public bool iapUseRealStoreInEditor = false;
        [Tooltip("Android only: Base64 RSA public key from Play Console → Monetize with Play → Monetization setup → Licensing. Nexus generates GooglePlayTangle.cs automatically.")]
        public string googlePlayLicensingKey = "";
        [Tooltip("Your products. The id must match exactly what you create in App Store Connect and Google Play Console.")]
        public List<NexusProduct> iapProducts = new List<NexusProduct>();

        [Header("Ads (AppLovin MAX)")]
        [Tooltip("Master switch. When off, NexusAds stays uninitialized and no ad calls are made. Requires the AppLovin MAX SDK (DAVANCI_ADS).")]
        public bool enableAds = false;
        [Tooltip("Initialize ads automatically at startup (recommended). Off if you'd rather call DavanciMonetization.InitializeAds() yourself.")]
        public bool adsAutoInitialize = true;
        [Tooltip("Show AppLovin test ads and enable the mediation debugger build. Turn OFF for production / store builds.")]
        public bool adsTestMode = true;
        [Tooltip("LevelPlay ONLY: bring the SDK up in Test-Suite mode so the Mediation Test Suite works. While ON, real ads do NOT serve (every load returns 509 No-fill) — this is an ironSource constraint. Turn OFF to serve real ads.")]
        public bool adsTestSuiteMode = false;
        [Tooltip("Your AppLovin SDK key (AppLovin dashboard → Account → Keys). One key for both platforms.")]
        public string applovinSdkKey = "";

        [Tooltip("Rewarded ad unit IDs from the AppLovin dashboard (one per platform).")]
        public string iosRewardedAdUnitId = "";
        public string androidRewardedAdUnitId = "";

        [Tooltip("Interstitial ad unit IDs from the AppLovin dashboard (one per platform).")]
        public string iosInterstitialAdUnitId = "";
        public string androidInterstitialAdUnitId = "";

        [Tooltip("Banner / in-game (MREC) ad unit IDs from the AppLovin dashboard (one per platform).")]
        public string iosBannerAdUnitId = "";
        public string androidBannerAdUnitId = "";

        [Header("Ads — Mediation")]
        [Tooltip("Which mediation SDK serves ads at runtime. Both can be installed; this picks the active one. Each uses its own keys + ad unit IDs.")]
        public NexusMediation adMediation = NexusMediation.AppLovinMax;

        [Header("Ads (LevelPlay / ironSource)")]
        [Tooltip("LevelPlay App Key (ironSource dashboard → your app). One per platform. Requires the LevelPlay SDK (DAVANCI_LEVELPLAY).")]
        public string iosLevelPlayAppKey = "";
        public string androidLevelPlayAppKey = "";

        [Tooltip("LevelPlay Rewarded ad unit IDs (one per platform).")]
        public string iosLpRewardedAdUnitId = "";
        public string androidLpRewardedAdUnitId = "";

        [Tooltip("LevelPlay Interstitial ad unit IDs (one per platform).")]
        public string iosLpInterstitialAdUnitId = "";
        public string androidLpInterstitialAdUnitId = "";

        [Tooltip("LevelPlay Banner / MREC ad unit IDs (one per platform).")]
        public string iosLpBannerAdUnitId = "";
        public string androidLpBannerAdUnitId = "";

        public static NexusConfig Load() => Resources.Load<NexusConfig>(ResourcePath);
    }
}
