using System.Collections.Generic;
using System.IO;
using CounterDrone.Core;
using CounterDrone.Core.Models;
using CounterDrone.Core.Repository;
using CounterDrone.Core.Services;
using UnityEngine;

namespace CounterDrone.Unity
{
    /// <summary>模型管理桥接 — 导入/删除/查询 3D 模型元数据</summary>
    public class ModelManager : MonoBehaviour
    {
        private IModelService _service;
        private IPathProvider _paths;
        private SQLite.SQLiteConnection _db;

        public IModelService Service => _service;

        public void Awake()
        {
            _paths = new UnityPathProvider();
            _db = new DatabaseManager(_paths).OpenMainDb();
            SqliteConnectionTracker.Track(_db);
            _service = new ModelService(new ModelRepository(_db), _paths);
        }

        void OnDisable() { SqliteConnectionTracker.Untrack(_db); _db?.Dispose(); _db = null; }

        public ModelInfo Import(string filePath, string name) => _service.ImportModel(filePath, name);
        public void Delete(string id) => _service.DeleteModel(id);
        public List<ModelInfo> GetAll() => _service.GetAllModels();
        public ModelInfo Get(string id) => _service.GetModel(id);

        [ContextMenu("Verify")]
        void Verify()
        {
            Awake();
            Debug.Log($"ModelManager OK. DB: {_paths.GetMainDbPath()}");
        }
    }
}
