import 'package:flutter/material.dart';

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

  static final List<Map<String, dynamic>> _mockMessages = [
    {'name': '王阿姨', 'preview': '今天的太极活动您要来吗？', 'time': '09:40', 'avatar': 8, 'unread': 2},
    {'name': '李老师', 'preview': '书法课作业我已经发群里啦。', 'time': '昨天', 'avatar': 15, 'unread': 0},
    {'name': '陈叔', 'preview': '周末一起去人民公园拍照吧。', 'time': '昨天', 'avatar': 21, 'unread': 1},
    {'name': '健康服务助手', 'preview': '您的问诊报告已更新，请及时查看。', 'time': '周日', 'avatar': 29, 'unread': 0},
    {'name': '张姐', 'preview': '广场舞队今晚7点老地方集合。', 'time': '周六', 'avatar': 35, 'unread': 5},
    {'name': '社区活动中心', 'preview': '您报名的茶话会还有2个名额。', 'time': '02/12', 'avatar': 42, 'unread': 0},
    {'name': '赵大哥', 'preview': '下棋那局改天再战，哈哈。', 'time': '02/09', 'avatar': 47, 'unread': 0},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFFFF8F0),
      appBar: AppBar(title: const Text('消息')),
      body: ListView.separated(
        padding: const EdgeInsets.all(16),
        itemCount: _mockMessages.length,
        separatorBuilder: (_, __) => const SizedBox(height: 10),
        itemBuilder: (context, index) => _buildMessageTile(_mockMessages[index]),
      ),
    );
  }

  Widget _buildMessageTile(Map<String, dynamic> message) {
    final unread = message['unread'] as int;
    return InkWell(
      borderRadius: BorderRadius.circular(14),
      onTap: () {
        // TODO: 跳转会话详情页
      },
      child: Container(
        padding: const EdgeInsets.all(12),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(14),
        ),
        child: Row(
          children: [
            CircleAvatar(
              radius: 24,
              backgroundColor: const Color(0xFFFFE7CF),
              backgroundImage: NetworkImage('https://i.pravatar.cc/50?img=${message['avatar']}'),
            ),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    message['name'] as String,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w700,
                      color: Color(0xFF333333),
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    message['preview'] as String,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(
                      fontSize: 14,
                      color: Color(0xFF8A8A8A),
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(width: 12),
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  message['time'] as String,
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF999999),
                  ),
                ),
                const SizedBox(height: 8),
                unread > 0
                    ? Container(
                        width: 18,
                        height: 18,
                        decoration: const BoxDecoration(
                          color: Color(0xFFFF4D4F),
                          shape: BoxShape.circle,
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          '$unread',
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 10,
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                      )
                    : const SizedBox(height: 18),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
