import { useEffect, useMemo, useState } from 'react';
import { ScrollView, Text, View, Pressable } from 'react-native';
import { useRouter } from 'expo-router';
import { api } from '../../src/api';
import type { DisposalPoint } from '../../src/types';
import { C, Field, styles, Loading } from '../../src/ui';
export default function Disposal(){const[points,setPoints]=useState<DisposalPoint[]>([]);const[q,setQ]=useState('');const[loading,setLoading]=useState(true);const r=useRouter();useEffect(()=>{api.points().then(setPoints).finally(()=>setLoading(false))},[]);const filtered=useMemo(()=>points.filter(p=>`${p.name} ${p.materials.join(' ')} ${p.address}`.toLowerCase().includes(q.toLowerCase())),[points,q]);if(loading)return <Loading/>;return <ScrollView style={styles.screen}><View style={styles.container}><Text style={styles.title}>Descarte correto</Text><Text style={styles.subtitle}>Encontre um local adequado para o seu resíduo.</Text><Field label="Pesquisar material ou local" placeholder="Ex.: bateria, papel, eletrônico..." value={q} onChangeText={setQ}/>{filtered.map(p=><Pressable key={p.id} onPress={()=>r.push({pathname:'/home/ponto',params:{id:p.id}})} style={styles.card}><Text style={{fontSize:17,fontWeight:'800',color:C.dark}}>{p.name}</Text><Text style={styles.small}>{p.address}</Text><Text style={{marginTop:10,color:'#42554C'}}>{p.materials.join(' • ')}</Text><Text style={[styles.small,{marginTop:8}]}>{p.opening_hours}</Text></Pressable>)}</View></ScrollView>}
