import 'package:flutter/foundation.dart';
import '../models/file_item.dart';
import '../services/file_service.dart';

class FileExplorerViewModel extends ChangeNotifier {
  final FileService _fileService;

  FileExplorerViewModel(this._fileService);

  List<FileItem> _files = [];
  bool _isLoading = false;
  String? _error;
  String _currentPath = '/';

  List<FileItem> get files => _files;
  bool get isLoading => _isLoading;
  String? get error => _error;
  String get currentPath => _currentPath;

  Future<void> loadFiles([String? path]) async {
    _isLoading = true;
    _error = null;
    if (path != null) _currentPath = path;
    notifyListeners();

    try {
      _files = await _fileService.getFiles("", _currentPath);
    } catch (e) {
      _error = e.toString();
      _files = [];
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }

  Future<void> uploadFile(FileItem file) async {
    try {
      await _fileService.uploadFile("", file);
      await loadFiles(); // Reload files
    } catch (e) {
      _error = e.toString();
      notifyListeners();
    }
  }

  Future<void> deleteFile(String path) async {
    try {
      await _fileService.deleteFile("", path);
      await loadFiles(); // Reload files
    } catch (e) {
      _error = e.toString();
      notifyListeners();
    }
  }
}
