const couchdbService = require("../services/couchdbService"); class Report { static async create(reportData) { const doc = { type: "report", ...reportData, status: reportData.status || "open", createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }; return await couchdbService.createDocument(doc); } static async findById(id) { const doc = await couchdbService.getDocument(id); if (doc && doc.type === "report") { return doc; } return null; } static async find(filter = {}) { const selector = { type: "report", ...filter, }; return await couchdbService.findDocuments(selector); } static async findWithPagination(options = {}) { const { page = 1, limit = 10, sort = { createdAt: -1 } } = options; const selector = { type: "report" }; return await couchdbService.findWithPagination(selector, { page, limit, sort, }); } static async update(id, updateData) { const doc = await couchdbService.getDocument(id); if (!doc || doc.type !== "report") { throw new Error("Report not found"); } const updatedDoc = { ...doc, ...updateData, updatedAt: new Date().toISOString(), }; return await couchdbService.updateDocument(id, updatedDoc); } static async delete(id) { const doc = await couchdbService.getDocument(id); if (!doc || doc.type !== "report") { throw new Error("Report not found"); } return await couchdbService.deleteDocument(id, doc._rev); } static async countDocuments(filter = {}) { const selector = { type: "report", ...filter, }; return await couchdbService.countDocuments(selector); } static async findByStreet(streetId) { const selector = { type: "report", "street._id": streetId, }; return await couchdbService.findDocuments(selector); } static async findByUser(userId) { const selector = { type: "report", "user._id": userId, }; return await couchdbService.findDocuments(selector); } static async findByStatus(status) { const selector = { type: "report", status, }; return await couchdbService.findDocuments(selector); } static async update(id, updateData) { return await couchdbService.update(id, updateData); } } module.exports = Report;