import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '/data/models/contact_model.dart';
import '/logic/contact_provider.dart';
import '/logic/auth_provider.dart';
import 'chat_screen.dart';

class NewChatScreen extends StatelessWidget {
  const NewChatScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final authProvider = context.watch<AuthProvider>();
    final contactProvider = context.watch<ContactProvider>();

    final filteredContacts = contactProvider.contacts
        .where((contact) => contact.id != authProvider.currentUserId)
        .toList();

    return Scaffold(
      appBar: AppBar(
        title: const Text('Новый чат'),
      ),
      body: contactProvider.isLoading
          ? const Center(child: CircularProgressIndicator())
          : contactProvider.error != null
              ? Center(child: Text('Error: ${contactProvider.error}'))
              : ListView.builder(
                  itemCount: filteredContacts.length,
                  itemBuilder: (context, index) {
                    final contact = filteredContacts[index];
                    return ListTile(
                      leading: CircleAvatar(
                        child: Text(contact.name[0]),
                      ),
                      title: Text(contact.name),
                      onTap: () {
                        // Создать чат с этим контактом
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (_) => ChatScreen(contact: contact),
                          ),
                        );
                      },
                    );
                  },
                ),
    );
  }
}