package com.example.ui.screens import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.* import androidx.compose.material.icons.outlined.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.example.data.model.* import com.example.ui.components.VerificationBadge import com.example.ui.theme.* @Composable fun SearchScreen( companies: List, searchQuery: String, onSearchQueryChange: (String) -> Unit, selectedCategory: String?, onSelectCategory: (String?) -> Unit, selectedDivision: String?, onSelectDivision: (String?) -> Unit, selectedRisk: VerificationStatus?, onSelectRisk: (VerificationStatus?) -> Unit, lang: AppLanguage, onSelectCompany: (String) -> Unit, onStartReportWithEntity: (String) -> Unit, modifier: Modifier = Modifier ) { val categories = listOf("E-commerce", "Visa & Overseas Jobs", "Microfinance / Loans", "Tech & Logistics", "Fake E-commerce") val divisions = listOf("Dhaka", "Chittagong", "Sylhet", "Rajshahi") val quickSuggestions = listOf("Pathao", "Daraz", "Quick Loan Express", "Euro Visa", "BD Gadget Hub", "Chaldal") // Filter logic val filteredCompanies = remember(companies, searchQuery, selectedCategory, selectedDivision, selectedRisk) { companies.filter { comp -> val matchQuery = if (searchQuery.isBlank()) true else { comp.nameEn.contains(searchQuery, ignoreCase = true) || comp.nameBn.contains(searchQuery, ignoreCase = true) || comp.rjscRegNo.contains(searchQuery, ignoreCase = true) || comp.tradeLicenseNo.contains(searchQuery, ignoreCase = true) || comp.phone.contains(searchQuery, ignoreCase = true) || comp.bkashMerchantNo.contains(searchQuery, ignoreCase = true) } val matchCategory = selectedCategory == null || comp.category.equals(selectedCategory, ignoreCase = true) val matchDivision = selectedDivision == null || comp.city.equals(selectedDivision, ignoreCase = true) val matchRisk = selectedRisk == null || comp.status == selectedRisk matchQuery && matchCategory && matchDivision && matchRisk } } Column( modifier = modifier .fillMaxSize() .background(MaterialTheme.colorScheme.background) ) { // Persistent Search Header Surface( color = BrandNavyPrimary, modifier = Modifier.fillMaxWidth() ) { Column( modifier = Modifier .fillMaxWidth() .padding(horizontal = 16.dp, vertical = 12.dp) ) { // Outlined Search Input OutlinedTextField( value = searchQuery, onValueChange = onSearchQueryChange, placeholder = { Text( text = if (lang == AppLanguage.BN) "কোম্পানির নাম, ট্রেড লাইসেন্স বা বিকাশ নম্বর..." else "Search company, Trade Lic, or bKash...", style = MaterialTheme.typography.bodyMedium.copy(color = LightTextMuted) ) }, leadingIcon = { Icon( imageVector = Icons.Default.Search, contentDescription = "Search", tint = BrandNavyPrimary ) }, trailingIcon = { if (searchQuery.isNotBlank()) { IconButton(onClick = { onSearchQueryChange("") }) { Icon( imageVector = Icons.Default.Clear, contentDescription = "Clear", tint = LightTextMuted ) } } }, singleLine = true, shape = RoundedCornerShape(12.dp), colors = OutlinedTextFieldDefaults.colors( focusedContainerColor = Color.White, unfocusedContainerColor = Color.White, focusedBorderColor = BrandBlueAccent, unfocusedBorderColor = Color.Transparent ), modifier = Modifier .fillMaxWidth() .testTag("input_persistent_search") ) Spacer(modifier = Modifier.height(10.dp)) // Quick Suggestion Chips Row LazyRow( horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth() ) { items(quickSuggestions) { suggestion -> Surface( onClick = { onSearchQueryChange(suggestion) }, shape = RoundedCornerShape(16.dp), color = Color.White.copy(alpha = 0.18f), border = androidx.compose.foundation.BorderStroke(1.dp, Color.White.copy(alpha = 0.3f)) ) { Text( text = suggestion, style = MaterialTheme.typography.labelSmall.copy( color = Color.White, fontSize = 11.sp, fontWeight = FontWeight.Medium ), modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp) ) } } } } } // Filter Bar (Category, City, Risk Status) Surface( color = MaterialTheme.colorScheme.surface, tonalElevation = 2.dp, modifier = Modifier.fillMaxWidth() ) { Column(modifier = Modifier.padding(vertical = 10.dp)) { // Category Filter Row LazyRow( contentPadding = PaddingValues(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp) ) { item { FilterChip( selected = selectedCategory == null, onClick = { onSelectCategory(null) }, label = { Text(if (lang == AppLanguage.BN) "সকল ক্যাটাগরি" else "All Categories") }, colors = FilterChipDefaults.filterChipColors( selectedContainerColor = BrandNavyPrimary, selectedLabelColor = Color.White ) ) } items(categories) { cat -> FilterChip( selected = selectedCategory == cat, onClick = { onSelectCategory(cat) }, label = { Text(cat) }, colors = FilterChipDefaults.filterChipColors( selectedContainerColor = BrandNavyPrimary, selectedLabelColor = Color.White ) ) } } Spacer(modifier = Modifier.height(6.dp)) // Division & Risk Chips LazyRow( contentPadding = PaddingValues(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp) ) { item { FilterChip( selected = selectedRisk == VerificationStatus.VERIFIED_SAFE, onClick = { onSelectRisk(if (selectedRisk == VerificationStatus.VERIFIED_SAFE) null else VerificationStatus.VERIFIED_SAFE) }, label = { Text(if (lang == AppLanguage.BN) "🛡️ নিরাপদ (Safe)" else "🛡️ Verified Safe") }, colors = FilterChipDefaults.filterChipColors( selectedContainerColor = StatusVerifiedGreen, selectedLabelColor = Color.White ) ) } item { FilterChip( selected = selectedRisk == VerificationStatus.BLACKLISTED_SCAM, onClick = { onSelectRisk(if (selectedRisk == VerificationStatus.BLACKLISTED_SCAM) null else VerificationStatus.BLACKLISTED_SCAM) }, label = { Text(if (lang == AppLanguage.BN) "🚫 কালো তালিকাভুক্ত (Scam)" else "🚫 Blacklisted") }, colors = FilterChipDefaults.filterChipColors( selectedContainerColor = StatusWarningRed, selectedLabelColor = Color.White ) ) } items(divisions) { div -> FilterChip( selected = selectedDivision == div, onClick = { onSelectDivision(if (selectedDivision == div) null else div) }, label = { Text(div) } ) } } } } // Search Results List if (filteredCompanies.isEmpty()) { // Empty State Box( modifier = Modifier .fillMaxSize() .padding(24.dp), contentAlignment = Alignment.Center ) { Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxWidth() ) { Box( modifier = Modifier .size(72.dp) .clip(RoundedCornerShape(20.dp)) .background(LightSurfaceVariant), contentAlignment = Alignment.Center ) { Icon( imageVector = Icons.Outlined.SearchOff, contentDescription = null, tint = LightTextMuted, modifier = Modifier.size(36.dp) ) } Spacer(modifier = Modifier.height(16.dp)) Text( text = if (lang == AppLanguage.BN) "কোনো রেকর্ড পাওয়া যায়নি" else "No Matching Entity Found", style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold) ) Spacer(modifier = Modifier.height(6.dp)) Text( text = if (lang == AppLanguage.BN) "সংশ্লিষ্ট প্রতিষ্ঠানটি সরকারের RJSC বা বৈধ ডেটাবেজে তালিকাভুক্ত নাও হতে পারে। টাকা পাঠানোর আগে সতর্ক থাকুন।" else "This company or number is not in the verified database. Exercise extreme caution before sending money.", style = MaterialTheme.typography.bodyMedium.copy( color = LightTextSecondary, textAlign = TextAlign.Center ), modifier = Modifier.padding(horizontal = 16.dp) ) Spacer(modifier = Modifier.height(20.dp)) Button( onClick = { onStartReportWithEntity(searchQuery) }, colors = ButtonDefaults.buttonColors(containerColor = StatusWarningRed), shape = RoundedCornerShape(10.dp), modifier = Modifier.testTag("btn_empty_state_report") ) { Icon( imageVector = Icons.Default.ReportProblem, contentDescription = null, modifier = Modifier.size(18.dp) ) Spacer(modifier = Modifier.width(6.dp)) Text( text = if (lang == AppLanguage.BN) "'$searchQuery' এর বিরুদ্ধে রিপোর্ট করুন" else "Report Suspicious Entity" ) } } } } else { LazyColumn( modifier = Modifier .fillMaxSize() .padding(horizontal = 16.dp), contentPadding = PaddingValues(top = 12.dp, bottom = 80.dp), verticalArrangement = Arrangement.spacedBy(12.dp) ) { item { Text( text = if (lang == AppLanguage.BN) "পাওয়া গেছে: ${filteredCompanies.size} টি ফলাফল" else "Showing ${filteredCompanies.size} verification results", style = MaterialTheme.typography.labelMedium.copy( color = LightTextMuted, fontWeight = FontWeight.Medium ) ) } items(filteredCompanies) { comp -> SearchResultCard( company = comp, lang = lang, onClick = { onSelectCompany(comp.id) }, onReport = { onStartReportWithEntity(comp.nameEn) } ) } } } } } @Composable fun SearchResultCard( company: Company, lang: AppLanguage, onClick: () -> Unit, onReport: () -> Unit, modifier: Modifier = Modifier ) { Surface( onClick = onClick, shape = RoundedCornerShape(14.dp), color = MaterialTheme.colorScheme.surface, border = androidx.compose.foundation.BorderStroke( 1.dp, if (company.status == VerificationStatus.BLACKLISTED_SCAM) StatusWarningRed.copy(alpha = 0.4f) else LightOutline ), shadowElevation = 2.dp, modifier = modifier .fillMaxWidth() .testTag("search_result_${company.id}") ) { Column(modifier = Modifier.padding(14.dp)) { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.Top ) { Column(modifier = Modifier.weight(1f)) { Text( text = if (lang == AppLanguage.BN) company.nameBn else company.nameEn, style = MaterialTheme.typography.titleMedium.copy( fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onSurface ) ) Text( text = "${company.category} • ${company.city}", style = MaterialTheme.typography.bodySmall.copy( color = LightTextMuted, fontSize = 12.sp ) ) } Spacer(modifier = Modifier.width(8.dp)) VerificationBadge(status = company.status, lang = lang) } Spacer(modifier = Modifier.height(10.dp)) Surface( color = LightSurfaceVariant, shape = RoundedCornerShape(8.dp), modifier = Modifier.fillMaxWidth() ) { Row( modifier = Modifier .fillMaxWidth() .padding(horizontal = 10.dp, vertical = 6.dp), horizontalArrangement = Arrangement.SpaceBetween ) { Text( text = "RJSC: ${company.rjscRegNo}", style = MaterialTheme.typography.labelSmall.copy( color = LightTextSecondary, fontWeight = FontWeight.Medium ) ) Text( text = "ঝুঁকি: ${company.riskScore}/100", style = MaterialTheme.typography.labelSmall.copy( color = if (company.riskScore > 60) StatusWarningRed else StatusVerifiedGreen, fontWeight = FontWeight.Bold ) ) } } Spacer(modifier = Modifier.height(8.dp)) Text( text = if (lang == AppLanguage.BN) company.summaryBn else company.summaryEn, style = MaterialTheme.typography.bodySmall.copy( color = LightTextSecondary, lineHeight = 17.sp ), maxLines = 2, overflow = TextOverflow.Ellipsis ) Spacer(modifier = Modifier.height(12.dp)) Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.CenterVertically ) { if (company.status == VerificationStatus.BLACKLISTED_SCAM || company.status == VerificationStatus.HIGH_RISK) { TextButton( onClick = onReport, colors = ButtonDefaults.textButtonColors(contentColor = StatusWarningRed) ) { Icon( imageVector = Icons.Default.AddAlert, contentDescription = null, modifier = Modifier.size(16.dp) ) Spacer(modifier = Modifier.width(4.dp)) Text( text = if (lang == AppLanguage.BN) "অভিযোগ যোগ করুন" else "Add Report", style = MaterialTheme.typography.labelSmall.copy(fontWeight = FontWeight.Bold) ) } } FilledTonalButton( onClick = onClick, shape = RoundedCornerShape(8.dp), colors = ButtonDefaults.filledTonalButtonColors( containerColor = BrandNavyPrimary.copy(alpha = 0.1f), contentColor = BrandNavyPrimary ) ) { Text( text = if (lang == AppLanguage.BN) "পূর্ণ প্রোফাইল দেখুন" else "View Profile", style = MaterialTheme.typography.labelSmall.copy(fontWeight = FontWeight.Bold) ) } } } } }