import 'dart:io'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_storage/firebase_storage.dart'; import '../models/project_model.dart'; import '../models/branch_model.dart'; import '../models/project_doc_model.dart'; class ProjectService { FirebaseFirestore get _db => FirebaseFirestore.instance; FirebaseStorage get _storage => FirebaseStorage.instance; Stream> getProjects() { return _db.collection('projects') .orderBy('createdAt', descending: true) .snapshots() .map((snapshot) => snapshot.docs.map((doc) => ProjectModel.fromMap(doc.id, doc.data())).toList()); } Future addProject(ProjectModel project) async { await _db.collection('projects').add(project.toMap()); } Future updateProject(ProjectModel project) async { await _db.collection('projects').doc(project.id).update(project.toMap()); } Future deleteProject(String id) async { // Note: In real app, consider deleting subcollections and storage files too. await _db.collection('projects').doc(id).delete(); } // Branches Stream> getBranches(String projectId) { return _db.collection('projects').doc(projectId).collection('branches') .snapshots() .map((snapshot) => snapshot.docs.map((doc) => BranchModel.fromMap(doc.id, doc.data())).toList()); } Future> getBranchesOnce(String projectId) async { var snapshot = await _db.collection('projects').doc(projectId).collection('branches').get(); return snapshot.docs.map((doc) => BranchModel.fromMap(doc.id, doc.data())).toList(); } Future addBranch(String projectId, String name) async { await _db.collection('projects').doc(projectId).collection('branches').add({ 'name': name }); } Future deleteBranch(String projectId, String branchId) async { await _db.collection('projects').doc(projectId).collection('branches').doc(branchId).delete(); } // Docs Stream> getDocs(String projectId) { return _db.collection('projects').doc(projectId).collection('docs') .orderBy('uploadedAt', descending: true) .snapshots() .map((snapshot) => snapshot.docs.map((doc) => ProjectDocModel.fromMap(doc.id, doc.data())).toList()); } Future addDoc(String projectId, String label, String url, String type) async { await _db.collection('projects').doc(projectId).collection('docs').add({ 'label': label, 'url': url, 'type': type, 'uploadedAt': FieldValue.serverTimestamp(), }); } Future deleteDoc(String projectId, String docId) async { await _db.collection('projects').doc(projectId).collection('docs').doc(docId).delete(); } Future uploadDoc(String projectId, File file) async { String fileName = "${DateTime.now().millisecondsSinceEpoch}_${file.path.split(Platform.pathSeparator).last}"; var ref = _storage.ref().child('projects').child(projectId).child('docs').child(fileName); var uploadTask = await ref.putFile(file); return await uploadTask.ref.getDownloadURL(); } }