package com.example.data.repository import com.example.data.local.* import com.example.data.model.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.withContext class SatarkRepository(private val database: AppDatabase) { private val companyDao = database.companyDao() private val reportDao = database.scamReportDao() private val warningDao = database.publicWarningDao() private val notifDao = database.notificationDao() val allCompanies: Flow> = companyDao.getAllCompanies() val allReports: Flow> = reportDao.getAllReports() val allWarnings: Flow> = warningDao.getAllWarnings() val allNotifications: Flow> = notifDao.getAllNotifications() fun searchCompanies(query: String): Flow> { return if (query.isBlank()) { companyDao.getAllCompanies() } else { companyDao.searchCompanies(query.trim()) } } suspend fun getCompanyById(id: String): Company? = withContext(Dispatchers.IO) { companyDao.getCompanyById(id) } suspend fun getReportById(id: String): ScamReport? = withContext(Dispatchers.IO) { reportDao.getReportById(id) } suspend fun submitReport(report: ScamReport) = withContext(Dispatchers.IO) { reportDao.insertReport(report) // Also insert notification for user confirmation notifDao.insertNotification( AppNotification( id = "NOTIF-${System.currentTimeMillis()}", titleEn = "Complaint Lodged: ${report.id}", titleBn = "অভিযোগ গৃহীত হয়েছে: ${report.id}", messageEn = "Your report against '${report.targetEntityName}' is now under verification by our verification team.", messageBn = "'${report.targetEntityName}' এর বিরুদ্ধে আপনার রিপোর্টটি যাচাইকরণ প্রক্রিয়ায় রয়েছে।", timestamp = "Just now", type = "STATUS_UPDATE", isRead = false, relatedEntityId = report.id ) ) } suspend fun updateReportStatus( reportId: String, newStatus: ReportStatus, adminNotes: String?, isPublicAlert: Boolean = false ) = withContext(Dispatchers.IO) { val existing = reportDao.getReportById(reportId) if (existing != null) { val updated = existing.copy( status = newStatus, adminNotes = adminNotes ?: existing.adminNotes, isPublicAlertIssued = isPublicAlert ) reportDao.updateReport(updated) // If public alert issued, create public warning if (isPublicAlert && newStatus == ReportStatus.VERIFIED_WARNING_ISSUED) { warningDao.insertWarning( PublicWarning( id = "WARN-${System.currentTimeMillis()}", titleEn = "Verified Scam Alert: ${existing.targetEntityName}", titleBn = "যাচাইকৃত প্রতারণা সতর্কতা: ${existing.targetEntityName}", category = existing.scamCategory, severity = AlertSeverity.CRITICAL_RED, targetName = existing.targetEntityName, date = "Today", division = existing.city, victimsCount = 1, estimatedLossBdt = "৳${existing.moneyLostBdt.toLong()}", summaryEn = existing.incidentDescription, summaryBn = existing.incidentDescription, modusOperandiEn = "Reported by citizen. Verified bKash/Nagad transactions and forged credentials.", modusOperandiBn = "নাগরিক রিপোর্ট ও যাচাইকৃত প্রমাণাদির ভিত্তিতে সতর্কতা জারি করা হলো।", officialNoticeRef = "Satark BD Consumer Shield Verification #2026", isPinned = false ) ) } } } suspend fun markAllNotificationsRead() = withContext(Dispatchers.IO) { notifDao.markAllAsRead() } suspend fun initializeSeedDataIfEmpty() = withContext(Dispatchers.IO) { val existingCompanies = companyDao.getAllCompanies().first() if (existingCompanies.isEmpty()) { companyDao.insertCompanies(SeedData.companies) } val existingReports = reportDao.getAllReports().first() if (existingReports.isEmpty()) { reportDao.insertReports(SeedData.scamReports) } val existingWarnings = warningDao.getAllWarnings().first() if (existingWarnings.isEmpty()) { warningDao.insertWarnings(SeedData.publicWarnings) } val existingNotifs = notifDao.getAllNotifications().first() if (existingNotifs.isEmpty()) { notifDao.insertNotifications(SeedData.notifications) } } }