import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../i18n/strings.g.dart';
import '../services/message_store.dart';
import 'theme.dart';

/// The messages inbox: past conversations, newest first. Tapping one opens the
/// chat. Reached from the drawer's "Chat" entry.
class ChatListScreen extends StatefulWidget {
  const ChatListScreen({required this.store, super.key});

  final MessageStore store;

  @override
  State<ChatListScreen> createState() => _ChatListScreenState();
}

class _ChatListScreenState extends State<ChatListScreen> {
  List<ChatSummary>? _items;

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

  Future<void> _load() async {
    final items = await widget.store.conversations();
    if (mounted) setState(() => _items = items);
  }

  @override
  Widget build(BuildContext context) {
    final t = context.t;
    final items = _items;
    return Scaffold(
      appBar: AppBar(title: Text(t.chatList.title)),
      body: items == null
          ? const Center(child: CircularProgressIndicator())
          : items.isEmpty
              ? Center(
                  child: Padding(
                    padding: const EdgeInsets.all(32),
                    child: Text(
                      t.chatList.empty,
                      textAlign: TextAlign.center,
                      style: const TextStyle(color: seedMuted, fontSize: 15),
                    ),
                  ),
                )
              : ListView.separated(
                  itemCount: items.length,
                  separatorBuilder: (_, _) => const Divider(height: 1),
                  itemBuilder: (context, i) {
                    final c = items[i];
                    return ListTile(
                      leading: const CircleAvatar(
                        backgroundColor: seedAvatar,
                        child: Icon(Icons.person, color: seedOnAvatar),
                      ),
                      title: Text(_shortId(c.peerPubkey)),
                      subtitle: Text(
                        c.lastText,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      onTap: () => context.go('/chat/${c.peerPubkey}'),
                    );
                  },
                ),
    );
  }
}

/// A compact, human-ish rendering of a public key until we resolve names.
String _shortId(String pubkey) => pubkey.length <= 12
    ? pubkey
    : '${pubkey.substring(0, 6)}…${pubkey.substring(pubkey.length - 4)}';
