import 'package:flutter/material.dart';
import '../widgets/city_card.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  // Liste "source de vérité" affichée dans l'UI
  final List<Map<String, dynamic>> cities = [
    {'name': 'Paris', 'image': 'assets/images/paris.jpg', 'checked': false},
    {'name': 'Lyon', 'image': 'assets/images/lyon.jpg', 'checked': false},
    {'name': 'Nice', 'image': 'assets/images/nice.jpg', 'checked': false},
  ];

  // Fonction pour basculer l'état favori d'une ville
  void switchChecked(Map<String, dynamic> city) {
    setState(() {
      city['checked'] = !(city['checked'] ?? false);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const Icon(Icons.home),
        title: const Text('DrEvaristen Trip'),
        actions: const [Icon(Icons.more_vert)],
      ),
      body: Container(
        padding: const EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          // On transforme chaque map en CityCard
          children: [
            for (final city in cities)
              CityCard(
                name: city['name'],
                image: city['image'],
                checked: city['checked'] ?? false,
                updateChecked: () =>
                    switchChecked(city), // capture la bonne ville
              ),
          ],
        ),
      ),
    );
  }
}
