import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/constants/app_colors.dart';
import '../../providers/user_providers.dart';

class ProfilePage extends ConsumerWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final user = ref.watch(userNotifierProvider);

    return Scaffold(
      appBar: AppBar(
        title: const Text('我的'),
        backgroundColor: AppColors.primary,
        foregroundColor: Colors.white,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 用户信息卡片
            Container(
              width: double.infinity,
              padding: const EdgeInsets.all(24),
              decoration: const BoxDecoration(
                gradient: LinearGradient(
                  colors: [AppColors.primary, Color(0xFF16A34A)],
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
              ),
              child: Column(
                children: [
                  CircleAvatar(
                    radius: 40,
                    backgroundColor: Colors.white,
                    child: Text(
                      user.nickname?.substring(0, 1) ?? 'U',
                      style: const TextStyle(
                        fontSize: 32,
                        fontWeight: FontWeight.bold,
                        color: AppColors.primary,
                      ),
                    ),
                  ),
                  const SizedBox(height: 12),
                  Text(
                    user.nickname ?? '未设置昵称',
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    '账户序列: ${user.accountSequence ?? '-'}',
                    style: const TextStyle(color: Colors.white70, fontSize: 14),
                  ),
                ],
              ),
            ),

            const SizedBox(height: 16),

            // 菜单列表
            _buildMenuSection(
              title: '资产管理',
              items: [
                _MenuItem(
                  icon: Icons.history,
                  label: '挖矿记录',
                  onTap: () {},
                ),
                _MenuItem(
                  icon: Icons.receipt_long,
                  label: '交易记录',
                  onTap: () {},
                ),
                _MenuItem(
                  icon: Icons.swap_vert,
                  label: '划转记录',
                  onTap: () {},
                ),
              ],
            ),

            _buildMenuSection(
              title: '账户设置',
              items: [
                _MenuItem(
                  icon: Icons.person_outline,
                  label: '个人信息',
                  onTap: () {},
                ),
                _MenuItem(
                  icon: Icons.security,
                  label: '安全设置',
                  onTap: () {},
                ),
                _MenuItem(
                  icon: Icons.notifications_outlined,
                  label: '消息通知',
                  onTap: () {},
                ),
              ],
            ),

            _buildMenuSection(
              title: '其他',
              items: [
                _MenuItem(
                  icon: Icons.help_outline,
                  label: '帮助中心',
                  onTap: () {},
                ),
                _MenuItem(
                  icon: Icons.info_outline,
                  label: '关于我们',
                  onTap: () {},
                ),
              ],
            ),

            const SizedBox(height: 24),

            // 退出登录
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: OutlinedButton(
                onPressed: () {
                  ref.read(userNotifierProvider.notifier).logout();
                },
                style: OutlinedButton.styleFrom(
                  foregroundColor: AppColors.error,
                  side: const BorderSide(color: AppColors.error),
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  minimumSize: const Size(double.infinity, 48),
                ),
                child: const Text('退出登录'),
              ),
            ),

            const SizedBox(height: 32),
          ],
        ),
      ),
    );
  }

  Widget _buildMenuSection({
    required String title,
    required List<_MenuItem> items,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
          child: Text(
            title,
            style: const TextStyle(
              color: AppColors.textSecondary,
              fontSize: 12,
            ),
          ),
        ),
        Card(
          margin: const EdgeInsets.symmetric(horizontal: 16),
          child: Column(
            children: items.map((item) => _buildMenuItem(item)).toList(),
          ),
        ),
      ],
    );
  }

  Widget _buildMenuItem(_MenuItem item) {
    return ListTile(
      leading: Icon(item.icon, color: AppColors.textSecondary),
      title: Text(item.label),
      trailing: const Icon(Icons.chevron_right, color: AppColors.textMuted),
      onTap: item.onTap,
    );
  }
}

class _MenuItem {
  final IconData icon;
  final String label;
  final VoidCallback onTap;

  _MenuItem({
    required this.icon,
    required this.label,
    required this.onTap,
  });
}
