// lib/features/admin/tabs/messages/settings_panel.dart

import 'dart:convert';

import 'package:flutter/material.dart';

class SettingsPanel extends StatelessWidget {
  final VoidCallback onBack;
  final VoidCallback onProfileTap;
  final String myName;
  final String? myPic;

  const SettingsPanel({
    super.key,
    required this.onBack,
    required this.onProfileTap,
    required this.myName,
    this.myPic,
  });

  static const Color _green = Color.fromARGB(255, 2, 105, 48);
  static const Color _gold = Color.fromARGB(255, 204, 117, 4);
  static const Color _ink = Color(0xFF17231C);
  static const Color _muted = Color(0xFF66756C);
  static const Color _border = Color(0xFFE2E8E4);

  ImageProvider? _getAvatarProvider(String? imageString) {
    if (imageString == null || imageString.trim().isEmpty) return null;
    final value = imageString.trim();
    if (value.startsWith('http')) return NetworkImage(value);
    try {
      final clean = value.contains(',') ? value.split(',').last : value;
      return MemoryImage(base64Decode(clean.replaceAll(RegExp(r'\s+'), '')));
    } catch (_) {
      return null;
    }
  }

  Widget _settingItem({
    required IconData icon,
    required String title,
    required String subtitle,
    Color? iconColor,
  }) {
    return Material(
      color: Colors.white,
      borderRadius: BorderRadius.circular(14),
      child: InkWell(
        onTap: () {},
        borderRadius: BorderRadius.circular(14),
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 13),
          decoration: BoxDecoration(
            border: Border.all(color: _border),
            borderRadius: BorderRadius.circular(14),
          ),
          child: Row(
            children: [
              Container(
                width: 42,
                height: 42,
                decoration: BoxDecoration(
                  color: (iconColor ?? _green).withOpacity(.08),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Icon(icon, color: iconColor ?? _green, size: 22),
              ),
              const SizedBox(width: 13),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: const TextStyle(
                        color: _ink,
                        fontWeight: FontWeight.w800,
                        fontSize: 14.5,
                      ),
                    ),
                    const SizedBox(height: 3),
                    Text(
                      subtitle,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(
                        color: _muted,
                        fontSize: 11.5,
                        height: 1.3,
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(width: 8),
              const Icon(Icons.chevron_right_rounded, color: _muted),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final avatar = _getAvatarProvider(myPic);

    return ColoredBox(
      color: const Color(0xFFF5F7F6),
      child: SafeArea(
        top: false,
        child: Column(
          children: [
            Container(
              height: 66,
              padding: const EdgeInsets.symmetric(horizontal: 12),
              decoration: const BoxDecoration(
                color: Colors.white,
                border: Border(bottom: BorderSide(color: _border)),
              ),
              child: Row(
                children: [
                  IconButton(
                    tooltip: 'Back',
                    onPressed: onBack,
                    icon: const Icon(Icons.arrow_back_rounded, color: _ink),
                  ),
                  const SizedBox(width: 4),
                  const Expanded(
                    child: Text(
                      'Settings',
                      style: TextStyle(
                        color: _ink,
                        fontSize: 18,
                        fontWeight: FontWeight.w900,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: LayoutBuilder(
                builder: (context, constraints) {
                  final horizontal = constraints.maxWidth > 700
                      ? (constraints.maxWidth - 620) / 2
                      : 12.0;

                  return ListView(
                    physics: const BouncingScrollPhysics(),
                    padding: EdgeInsets.fromLTRB(horizontal, 16, horizontal, 28),
                    children: [
                      Material(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        child: InkWell(
                          onTap: onProfileTap,
                          borderRadius: BorderRadius.circular(16),
                          child: Container(
                            padding: const EdgeInsets.all(15),
                            decoration: BoxDecoration(
                              border: Border.all(color: _border),
                              borderRadius: BorderRadius.circular(16),
                            ),
                            child: Row(
                              children: [
                                CircleAvatar(
                                  radius: 30,
                                  backgroundColor: const Color(0xFFE7ECE9),
                                  backgroundImage: avatar,
                                  child: avatar == null
                                      ? const Icon(
                                          Icons.person_rounded,
                                          color: _green,
                                          size: 31,
                                        )
                                      : null,
                                ),
                                const SizedBox(width: 14),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        myName,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        style: const TextStyle(
                                          color: _ink,
                                          fontWeight: FontWeight.w900,
                                          fontSize: 17,
                                        ),
                                      ),
                                      const SizedBox(height: 4),
                                      const Text(
                                        'Available • View profile',
                                        style: TextStyle(
                                          color: _muted,
                                          fontSize: 12.5,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                const Icon(Icons.chevron_right_rounded, color: _muted),
                              ],
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(height: 18),
                      const Text(
                        'MESSAGING PREFERENCES',
                        style: TextStyle(
                          color: _green,
                          fontSize: 10.5,
                          letterSpacing: .9,
                          fontWeight: FontWeight.w900,
                        ),
                      ),
                      const SizedBox(height: 9),
                      _settingItem(
                        icon: Icons.account_circle_outlined,
                        title: 'Account',
                        subtitle: 'Identity and account preferences',
                      ),
                      const SizedBox(height: 9),
                      _settingItem(
                        icon: Icons.lock_outline_rounded,
                        title: 'Privacy',
                        subtitle: 'Control who can interact with you',
                      ),
                      const SizedBox(height: 9),
                      _settingItem(
                        icon: Icons.chat_bubble_outline_rounded,
                        title: 'Chats',
                        subtitle: 'Conversation display and chat preferences',
                      ),
                      const SizedBox(height: 9),
                      _settingItem(
                        icon: Icons.notifications_none_rounded,
                        title: 'Notifications',
                        subtitle: 'Message and activity alerts',
                        iconColor: _gold,
                      ),
                      const SizedBox(height: 9),
                      _settingItem(
                        icon: Icons.storage_rounded,
                        title: 'Storage and data',
                        subtitle: 'Manage local files and download behaviour',
                      ),
                      const SizedBox(height: 9),
                      _settingItem(
                        icon: Icons.help_outline_rounded,
                        title: 'Help',
                        subtitle: 'Support information and assistance',
                      ),
                    ],
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
