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

namespace CounterDrone.Core.Services
{
    public class ModelService : IModelService
    {
        private readonly ModelRepository _repo;
        private readonly IPathProvider _paths;

        public ModelService(ModelRepository repo, IPathProvider paths)
        {
            _repo = repo;
            _paths = paths;
        }

        public ModelInfo ImportModel(string filePath, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentException("模型名称不能为空");

            var ext = Path.GetExtension(filePath).ToLowerInvariant();
            if (ext != ".fbx" && ext != ".obj" && ext != ".stl" && ext != ".glb" && ext != ".gltf")
                throw new ArgumentException($"不支持的模型格式：{ext}");

            var fileInfo = new FileInfo(filePath);
            if (fileInfo.Length > 500 * 1024 * 1024)
                throw new ArgumentException("模型文件大小超过 500MB 限制");

            var model = new ModelInfo
            {
                Name = name,
                FilePath = filePath,
                FileSize = fileInfo.Length / (1024.0 * 1024.0),
            };

            _repo.Insert(model);
            return model;
        }

        public void DeleteModel(string id)
        {
            _repo.Delete(id);
        }

        public List<ModelInfo> GetAllModels()
        {
            return _repo.GetAll();
        }

        public ModelInfo GetModel(string id)
        {
            return _repo.GetById(id);
        }
    }
}
