

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace com.al_arcade.shared
{
    public class SSGameSession : MonoBehaviour
    {
        public static SSGameSession Instance { get; private set; }

        [Header("Build Configuration")]
        [Tooltip("scistreet = public, teacher = requires class code")]
        public string buildType = "scistreet";

        [Tooltip("Grade ID filter. 0 = all grades")]
        public int gradeId = 0;

        [Tooltip("Number of questions to fetch")]
        public int questionCount = 10;


        [HideInInspector] public string classCode = "";
        [HideInInspector] public string className = "";
        [HideInInspector] public string teacherName = "";
        [HideInInspector] public string gradeName = "";
        [HideInInspector] public bool isClassValidated = false;


        [Header("Events")]
        public UnityEvent onSessionReady;
        public UnityEvent<string> onSessionError;

        private void Awake()
        {
            if (Instance != null && Instance != this)
            {
                Destroy(gameObject);
                return;
            }
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }

        public static SSGameSession EnsureInstance()
        {
            if (Instance != null) return Instance;
            var go = new GameObject("[SSGameSession]");
            Instance = go.AddComponent<SSGameSession>();
            DontDestroyOnLoad(go);
            return Instance;
        }


        public IEnumerator ValidateAndStartTeacherSession(string code,
            Action onSuccess, Action<string> onError)
        {
            var api = SSApiManager.EnsureInstance();

            yield return api.ValidateClassCode(code,
                cls =>
                {
                    classCode = cls.class_code;
                    className = cls.class_name;
                    teacherName = cls.teacher_name;
                    gradeName = cls.grade_name;
                    buildType = "teacher";
                    isClassValidated = true;

                    if (int.TryParse(cls.grade_id, out int gid))
                        gradeId = gid;

                    Debug.Log($"[SSSession] Teacher session started: {className} by {teacherName}");
                    onSuccess?.Invoke();
                    onSessionReady?.Invoke();
                },
                err =>
                {
                    isClassValidated = false;
                    Debug.LogWarning($"[SSSession] Validation failed: {err}");
                    onError?.Invoke(err);
                    onSessionError?.Invoke(err);
                });
        }


        public void StartSciStreetSession(int grade = 0)
        {
            buildType = "scistreet";
            gradeId = grade;
            classCode = "";
            isClassValidated = true;
            Debug.Log("[SSSession] SciStreet session started");
            onSessionReady?.Invoke();
        }


        public void ResetSession()
        {
            buildType = "scistreet";
            gradeId = 0;
            classCode = "";
            className = "";
            teacherName = "";
            gradeName = "";
            isClassValidated = false;
        }
    }
}
