import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../core/app_colors.dart';
import '../../database/db_helper.dart';
import '../../models/models.dart';
import '../../services/auth_service.dart';

class CitizenReporteScreen extends StatefulWidget {
  const CitizenReporteScreen({super.key});
  @override State<CitizenReporteScreen> createState() => _CitizenReporteScreenState();
}

class _CitizenReporteScreenState extends State<CitizenReporteScreen> {
  String _tipo = 'CAMION_NO_PASO';
  final _desc = TextEditingController();
  int _calif = 5;
  bool _loading = false, _sent = false;
  List<ReporteModel> _reportes = [];

  static const _tipos = {
    'CAMION_NO_PASO':'🚛 El camión no pasó',
    'RETRASO':'⏱️ Retraso significativo',
    'RESIDUOS_NO_RECOGIDOS':'🗑️ Residuos no recogidos',
    'OTRO':'📝 Otro motivo',
  };

  @override void initState() { super.initState(); _load(); }

  Future<void> _load() async {
    final auth = context.read<AuthService>();
    if (auth.currentUser == null) return;
    final r = await DbHelper.getReportesByUser(auth.currentUser!.id!);
    if (mounted) setState(() => _reportes = r);
  }

  Future<void> _send() async {
    final auth = context.read<AuthService>();
    if (auth.currentUser == null || _desc.text.trim().isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text('Describe el problema'), backgroundColor: AppColors.rojoError)); return;
    }
    setState(() => _loading = true);
    await DbHelper.insertReporte(ReporteModel(
      userId: auth.currentUser!.id!, tipo: _tipo, descripcion: _desc.text.trim(),
      colonia: auth.primaryDomicilio?.colonia ?? '',
      routeId: auth.primaryDomicilio?.routeId ?? '',
      fecha: DateTime.now().toIso8601String(), calificacion: _calif,
    ));
    await _load();
    if (!mounted) return;
    setState(() { _loading = false; _sent = true; _desc.clear(); });
    await Future.delayed(const Duration(seconds: 2));
    if (mounted) setState(() => _sent = false);
  }

  @override
  Widget build(BuildContext context) => Scaffold(
    backgroundColor: AppColors.grisFondo,
    appBar: AppBar(automaticallyImplyLeading: false,
      backgroundColor: AppColors.guindaPrimary, foregroundColor: Colors.white,
      title: const Text('Reportar Incidencia'),
      bottom: PreferredSize(preferredSize: const Size.fromHeight(4),
          child: Container(height: 4, color: AppColors.dorado))),
    body: _sent ? Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
      const Icon(Icons.check_circle, color: AppColors.verdeExito, size: 64),
      const SizedBox(height: 12),
      const Text('¡Reporte enviado!', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: AppColors.verdeExito)),
      const Text('El Ayuntamiento lo revisará pronto.', style: TextStyle(color: AppColors.grisTexto)),
    ])) : SingleChildScrollView(padding: const EdgeInsets.all(16), child: Column(children: [
      Card(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
        child: Padding(padding: const EdgeInsets.all(16), child: Column(
          crossAxisAlignment: CrossAxisAlignment.start, children: [
          const Text('Nueva Incidencia', style: TextStyle(fontWeight: FontWeight.bold, color: AppColors.guindaPrimary, fontSize: 16)),
          const SizedBox(height: 12),
          const Text('Tipo:', style: TextStyle(fontWeight: FontWeight.w600, fontSize: 13)),
          ..._tipos.entries.map((e) => RadioListTile<String>(dense: true, value: e.key,
              groupValue: _tipo, title: Text(e.value, style: const TextStyle(fontSize: 13)),
              activeColor: AppColors.guindaPrimary,
              onChanged: (v) => setState(() => _tipo = v!))),
          const SizedBox(height: 8),
          DropdownButtonFormField<int>(value: _calif,
            decoration: const InputDecoration(labelText: 'Calificación', border: OutlineInputBorder()),
            items: [5,4,3,2,1].map((n) => DropdownMenuItem(value: n,
                child: Text(['⭐⭐⭐⭐⭐ Excelente','⭐⭐⭐⭐ Bueno','⭐⭐⭐ Regular','⭐⭐ Malo','⭐ Muy malo'][5-n]))).toList(),
            onChanged: (v) => setState(() => _calif = v!)),
          const SizedBox(height: 10),
          TextField(controller: _desc, maxLines: 3,
            decoration: const InputDecoration(hintText: 'Describe el problema...',
                border: OutlineInputBorder(), filled: true, fillColor: Colors.white)),
          const SizedBox(height: 14),
          SizedBox(width: double.infinity, height: 48,
            child: ElevatedButton.icon(onPressed: _loading ? null : _send,
              style: ElevatedButton.styleFrom(backgroundColor: AppColors.guindaPrimary,
                  foregroundColor: Colors.white, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8))),
              icon: _loading ? const SizedBox(width: 18, height: 18,
                  child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2)) : const Icon(Icons.send),
              label: const Text('ENVIAR REPORTE', style: TextStyle(fontWeight: FontWeight.bold)))),
        ]))),
      if (_reportes.isNotEmpty) ...[
        const SizedBox(height: 16),
        const Align(alignment: Alignment.centerLeft,
          child: Text('Mis Reportes', style: TextStyle(fontWeight: FontWeight.bold, color: AppColors.guindaPrimary, fontSize: 15))),
        const SizedBox(height: 8),
        ..._reportes.map((r) => Card(margin: const EdgeInsets.only(bottom: 6),
          child: ListTile(dense: true,
            leading: const CircleAvatar(backgroundColor: AppColors.guindaPrimary, radius: 16,
              child: Icon(Icons.report, color: Colors.white, size: 16)),
            title: Text(_tipos[r.tipo] ?? r.tipo, style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w600)),
            subtitle: Text(r.descripcion, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 11)),
            trailing: Container(padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
              decoration: BoxDecoration(color: AppColors.naranjaAlerta.withOpacity(0.15), borderRadius: BorderRadius.circular(10)),
              child: Text(r.estado, style: const TextStyle(fontSize: 9, color: AppColors.naranjaAlerta, fontWeight: FontWeight.bold)))))),
      ],
    ])),
  );
  @override void dispose() { _desc.dispose(); super.dispose(); }
}
