import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'dart:io';
import '../blocs/auth/auth_bloc.dart';
import '../blocs/auth/auth_state.dart';
import '../blocs/auth/auth_event.dart';
import '../models/user.dart';
import '../services/api_client.dart';
import '../theme/app_theme.dart';
import '../theme/modern_glass_button.dart';

class AccountSettingsDialog extends StatefulWidget {
  const AccountSettingsDialog({super.key});

  @override
  State<AccountSettingsDialog> createState() => _AccountSettingsDialogState();
}

class _AccountSettingsDialogState extends State<AccountSettingsDialog> {
  int _selectedTabIndex = 0;
  bool _isLoading = false;
  bool _isUploadingAvatar = false;
  double _avatarUploadProgress = 0.0;
  String? _error;
  bool _hasChanges = false;

  // Profile fields
  late TextEditingController _displayNameController;
  String? _avatarUrl;

  // Security fields
  late TextEditingController _currentPasswordController;
  late TextEditingController _newPasswordController;
  late TextEditingController _confirmPasswordController;

  User? _currentUser;

  @override
  void initState() {
    super.initState();
    _displayNameController = TextEditingController();
    _displayNameController.addListener(() {
      if (mounted && _currentUser != null) {
        setState(
          () => _hasChanges =
              _displayNameController.text != (_currentUser!.displayName ?? ''),
        );
      }
    });
    _currentPasswordController = TextEditingController();
    _newPasswordController = TextEditingController();
    _confirmPasswordController = TextEditingController();

    // Get initial user data
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        final authState = context.read<AuthBloc>().state;
        if (authState is AuthAuthenticated) {
          _currentUser = authState.user;
          _displayNameController.text = _currentUser?.displayName ?? '';
          _avatarUrl = _currentUser?.avatarUrl;
          _hasChanges = false;
        }
      }
    });
  }

  @override
  void dispose() {
    _displayNameController.dispose();
    _currentPasswordController.dispose();
    _newPasswordController.dispose();
    _confirmPasswordController.dispose();
    super.dispose();
  }

  Future<void> _pickAndUploadAvatar() async {
    try {
      final result = await FilePicker.platform.pickFiles(
        type: FileType.image,
        allowMultiple: false,
        withData: true, // Ensure bytes are available for web
      );

      if (result != null && result.files.isNotEmpty) {
        final file = result.files.single;
        final bytes = file.bytes ?? await File(file.path!).readAsBytes();
        final filename = file.name;

        setState(() {
          _isUploadingAvatar = true;
          _avatarUploadProgress = 0.0;
        });

        try {
          final apiClient = GetIt.I<ApiClient>();
          final response = await apiClient.uploadAvatar(
            bytes,
            filename,
            onSendProgress: (sent, total) {
              if (total != -1 && mounted) {
                setState(() {
                  _avatarUploadProgress = sent / total;
                });
              }
            },
          );

          setState(() {
            _isUploadingAvatar = false;
            _avatarUploadProgress = 0.0;
          });

          // Immediately show a preview if the server returned the avatar bytes
          try {
            final avatarData = response['avatarData'] as String?;
            final contentType = response['contentType'] as String?;
            if (avatarData != null && contentType != null) {
              setState(() {
                _avatarUrl = 'data:$contentType;base64,$avatarData';
              });
            }
          } catch (_) {
            // ignore
          }

          // Fetch latest profile from backend so we get the canonical avatar URL and version
          try {
            final apiClient = GetIt.I<ApiClient>();
            final profile = await apiClient.getUserProfile();
            final newAvatar = profile['avatarUrl'] as String?;

            setState(() => _avatarUrl = newAvatar);

            if (profile.isNotEmpty && mounted) {
              final updatedUser = User.fromJson(profile);
              context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));
            }
          } catch (e) {
            // Fall back to response avatar URL if profile fetch fails
            setState(() {
              _avatarUrl = response['avatarUrl'] as String?;
              final authState = context.read<AuthBloc>().state;
              if (authState is AuthAuthenticated && _avatarUrl != null) {
                if (!_avatarUrl!.contains('token=')) {
                  _avatarUrl = "$_avatarUrl&token=${authState.token}";
                }
              }
              if (_currentUser != null && _avatarUrl != null) {
                final updatedUser = _currentUser!.copyWith(
                  avatarUrl: _avatarUrl,
                );
                if (mounted) {
                  context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));
                }
              }
            });
          }

          if (mounted) {
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text('Avatar uploaded successfully')),
            );
          }
        } catch (e) {
          setState(() {
            _isUploadingAvatar = false;
            _avatarUploadProgress = 0.0;
          });
          if (mounted) {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Failed to upload avatar: $e')),
            );
          }
        }
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('Failed to process image: $e')));
      }
    }
  }

  void _onSavePressed() {
    if (!_hasChanges || _isLoading) return;
    _updateProfile();
  }

  Future<void> _updateProfile() async {
    if (_currentUser == null) {
      return;
    }

    setState(() => _isLoading = true);
    try {
      final apiClient = GetIt.I<ApiClient>();
      final response = await apiClient.updateUserProfile(
        displayName: _displayNameController.text,
      );

      // Defensive: ensure response contains expected fields
      if (!response.containsKey('id')) {
        throw Exception('Unexpected response when updating profile');
      }

      // Use returned profile to update auth state (ensures avatarUrl + version are present)
      final updatedProfile = response;
      final updatedUser = User.fromJson(updatedProfile);

      if (mounted) {
        // Update auth state
        context.read<AuthBloc>().add(UpdateUserProfile(updatedUser));

        // Show success message
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Profile updated successfully')),
        );

        // Close the dialog
        Navigator.of(context).pop();
      }
    } catch (e) {
      if (mounted) {
        // Show error message
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('Failed to update profile: $e')));
      }
    } finally {
      setState(() => _isLoading = false);
    }
  }

  Future<void> _changePassword() async {
    if (_newPasswordController.text != _confirmPasswordController.text) {
      if (mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(const SnackBar(content: Text('Passwords do not match')));
      }
      return;
    }

    setState(() => _isLoading = true);
    try {
      final apiClient = GetIt.I<ApiClient>();
      await apiClient.changePassword(
        currentPassword: _currentPasswordController.text,
        newPassword: _newPasswordController.text,
      );
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Password changed successfully')),
        );
      }

      // Clear fields
      _currentPasswordController.clear();
      _newPasswordController.clear();
      _confirmPasswordController.clear();
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Failed to change password: $e')),
        );
      }
    } finally {
      setState(() => _isLoading = false);
    }
  }

  Future<void> _logout() async {
    context.read<AuthBloc>().add(const LogoutRequested());
    Navigator.of(context).pop();
  }

  void _showDeleteAccountConfirmation() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: AppTheme.primaryBackground,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
          child: Container(
            width: 400,
            padding: const EdgeInsets.all(24),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // Warning Icon
                Icon(
                  Icons.delete_forever,
                  color: AppTheme.errorColor,
                  size: 48,
                ),
                const SizedBox(height: 16),

                // Title
                Text(
                  'Delete Account',
                  style: TextStyle(
                    color: AppTheme.primaryText,
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 12),

                // Warning Message
                Text(
                  'Are you sure you want to delete your account?',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: AppTheme.primaryText,
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  'This will permanently delete your account and remove all your data from our servers. This action cannot be undone.',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: AppTheme.secondaryText,
                    fontSize: 14,
                    height: 1.4,
                  ),
                ),
                const SizedBox(height: 24),

                // Buttons
                Row(
                  children: [
                    Expanded(
                      child: TextButton(
                        onPressed: () => Navigator.of(context).pop(),
                        style: ButtonStyle(
                          splashFactory: NoSplash.splashFactory,
                          overlayColor: WidgetStateProperty.resolveWith<Color?>((
                            Set<WidgetState> states,
                          ) {
                            if (states.contains(WidgetState.pressed)) {
                              return Colors.transparent;
                            }
                            return null; // Use default for other states (like hover)
                          }),
                          padding: WidgetStateProperty.all(
                            const EdgeInsets.symmetric(vertical: 12),
                          ),
                          shape: WidgetStateProperty.all(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                              side: BorderSide(
                                color: AppTheme.secondaryText.withValues(
                                  alpha: 0.3,
                                ),
                              ),
                            ),
                          ),
                        ),
                        child: Text(
                          'Cancel',
                          style: TextStyle(
                            color: AppTheme.secondaryText,
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(width: 12),
                    Expanded(
                      child: ElevatedButton(
                        onPressed: () {
                          Navigator.of(context).pop();
                          _deleteAccount();
                        },
                        style: ButtonStyle(
                          splashFactory: NoSplash.splashFactory,
                          overlayColor: WidgetStateProperty.resolveWith<Color?>(
                            (Set<WidgetState> states) {
                              if (states.contains(WidgetState.pressed)) {
                                return Colors.transparent;
                              }
                              return null;
                            },
                          ),
                          backgroundColor: WidgetStateProperty.all(
                            AppTheme.errorColor,
                          ),
                          foregroundColor: WidgetStateProperty.all(
                            Colors.white,
                          ),
                          elevation: WidgetStateProperty.all(0),
                          shadowColor: WidgetStateProperty.all(
                            Colors.transparent,
                          ),
                          padding: WidgetStateProperty.all(
                            const EdgeInsets.symmetric(vertical: 12),
                          ),
                          shape: WidgetStateProperty.all(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                        ),
                        child: const Text(
                          'Delete Account',
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Future<void> _deleteAccount() async {
    setState(() => _isLoading = true);
    try {
      final apiClient = GetIt.I<ApiClient>();
      await apiClient.deleteAccount();

      if (mounted) {
        // Log out the user
        context.read<AuthBloc>().add(const LogoutRequested());
        Navigator.of(context).pop();

        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Account deleted successfully')),
        );
      }
    } catch (e) {
      setState(() => _error = e.toString());
      if (mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('Failed to delete account: $e')));
      }
    } finally {
      setState(() => _isLoading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, authState) {
        // Always ensure we have the current user
        if (authState is AuthAuthenticated) {
          _currentUser = authState.user;
          _avatarUrl = _currentUser?.avatarUrl;
          if (_avatarUrl != null && authState.token.isNotEmpty) {
            if (!_avatarUrl!.contains('token=')) {
              _avatarUrl = "$_avatarUrl&token=${authState.token}";
            }
          }
        }

        return Dialog(
          backgroundColor: AppTheme.primaryBackground,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
          child: Container(
            width: 500,
            height: 700,
            padding: const EdgeInsets.all(24),
            child: Column(
              children: [
                // Header
                Row(
                  children: [
                    Text(
                      'Account Settings',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Spacer(),
                    IconButton(
                      onPressed: () => Navigator.of(context).pop(),
                      icon: Icon(Icons.close, color: AppTheme.secondaryText),
                      splashColor: Colors.transparent,
                      highlightColor: Colors.transparent,
                    ),
                  ],
                ),
                const SizedBox(height: 16),

                // Tabs
                Row(
                  children: [
                    _buildTabButton('Profile', 0),
                    _buildTabButton('Security', 1),
                    _buildTabButton('Subscription', 2),
                  ],
                ),
                const SizedBox(height: 16),

                // Content
                Expanded(
                  child: _isLoading
                      ? Center(
                          child: CircularProgressIndicator(
                            color: AppTheme.accentColor,
                          ),
                        )
                      : _error != null
                      ? Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                _error!,
                                style: TextStyle(color: AppTheme.errorColor),
                              ),
                              const SizedBox(height: 16),
                              ModernGlassButton(
                                onPressed: () {
                                  setState(() => _error = null);
                                },
                                child: const Text('Retry'),
                              ),
                            ],
                          ),
                        )
                      : _buildTabContent(),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildTabButton(String text, int index) {
    final isSelected = _selectedTabIndex == index;
    return Expanded(
      child: GestureDetector(
        onTap: () => setState(() => _selectedTabIndex = index),
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
          margin: const EdgeInsets.symmetric(horizontal: 2),
          decoration: BoxDecoration(
            color: isSelected
                ? AppTheme.accentColor.withValues(alpha: 0.15)
                : Colors.transparent,
            borderRadius: BorderRadius.circular(12),
            border: Border.all(
              color: isSelected
                  ? AppTheme.accentColor
                  : AppTheme.secondaryText.withValues(alpha: 0.3),
              width: 1.5,
            ),
            boxShadow: isSelected
                ? [
                    BoxShadow(
                      color: AppTheme.accentColor.withValues(alpha: 0.3),
                      blurRadius: 8,
                      spreadRadius: 1,
                    ),
                  ]
                : null,
          ),
          child: AnimatedDefaultTextStyle(
            duration: const Duration(milliseconds: 200),
            style: TextStyle(
              color: isSelected ? AppTheme.accentColor : AppTheme.secondaryText,
              fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
              fontSize: 14,
            ),
            child: Text(text, textAlign: TextAlign.center),
          ),
        ),
      ),
    );
  }

  Widget _buildTabContent() {
    switch (_selectedTabIndex) {
      case 0:
        return _buildProfileTab();
      case 1:
        return _buildSecurityTab();
      case 2:
        return _buildSubscriptionTab();
      default:
        return const SizedBox.shrink();
    }
  }

  Widget _buildProfileTab() {
    return Stack(
      children: [
        SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Avatar
              Center(
                child: Column(
                  children: [
                    const SizedBox(height: 48),
                    GestureDetector(
                      onTap: _isUploadingAvatar ? null : _pickAndUploadAvatar,
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          CircleAvatar(
                            radius: 64,
                            backgroundColor: AppTheme.secondaryText.withValues(
                              alpha: 0.2,
                            ),
                            child: _avatarUrl != null
                                ? ClipOval(
                                    child: Image.network(
                                      _avatarUrl!,
                                      fit: BoxFit.cover,
                                      width: 100,
                                      height: 100,
                                      errorBuilder:
                                          (context, error, stackTrace) {
                                            return Icon(
                                              Icons.person,
                                              size: 50,
                                              color: AppTheme.secondaryText,
                                            );
                                          },
                                    ),
                                  )
                                : Icon(
                                    Icons.person,
                                    size: 50,
                                    color: AppTheme.secondaryText,
                                  ),
                          ),
                          if (_isUploadingAvatar)
                            SizedBox(
                              width: 110,
                              height: 110,
                              child: CircularProgressIndicator(
                                value: _avatarUploadProgress > 0
                                    ? _avatarUploadProgress
                                    : null,
                                strokeWidth: 4,
                                valueColor: AlwaysStoppedAnimation<Color>(
                                  AppTheme.accentColor,
                                ),
                                backgroundColor: AppTheme.secondaryText
                                    .withValues(alpha: 0.2),
                              ),
                            ),
                        ],
                      ),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      _isUploadingAvatar
                          ? 'Uploading avatar...'
                          : 'Tap to change avatar',
                      style: TextStyle(
                        color: AppTheme.secondaryText,
                        fontSize: 12,
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 24),

              // Display Name
              Text(
                'Display Name',
                style: TextStyle(
                  color: AppTheme.primaryText,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 8),
              Container(
                decoration: BoxDecoration(
                  color: AppTheme.primaryBackground.withValues(alpha: 0.5),
                  borderRadius: BorderRadius.circular(16),
                  border: Border.all(
                    color: AppTheme.accentColor.withValues(alpha: 0.3),
                  ),
                ),
                child: TextFormField(
                  controller: _displayNameController,
                  cursorColor: AppTheme.accentColor,
                  style: TextStyle(color: AppTheme.primaryText),
                  decoration: InputDecoration(
                    hintText: 'Enter display name',
                    hintStyle: TextStyle(color: AppTheme.secondaryText),
                    border: InputBorder.none,
                    contentPadding: const EdgeInsets.all(12),
                  ),
                ),
              ),
              const SizedBox(height: 24),

              // Save Button
              Center(
                child: SizedBox(
                  width: 144,
                  child: ModernGlassButton(
                    onPressed: _onSavePressed,
                    isLoading: _isLoading,
                    child: _isLoading
                        ? const SizedBox(
                            width: 20,
                            height: 20,
                            child: CircularProgressIndicator(
                              strokeWidth: 2,
                              valueColor: AlwaysStoppedAnimation<Color>(
                                Colors.white,
                              ),
                            ),
                          )
                        : const Text('Save Changes'),
                  ),
                ),
              ),
              const SizedBox(height: 2), // Space for logout button
            ],
          ),
        ),
        // Logout Button in bottom right corner of profile tab
        Positioned(
          bottom: 4,
          right: 4,
          child: IconButton(
            onPressed: _logout,
            icon: Icon(
              Icons.power_settings_new_rounded,
              color: AppTheme.errorColor,
            ),
            tooltip: 'Logout',
            iconSize: 28,
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
          ),
        ),
      ],
    );
  }

  Widget _buildSecurityTab() {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 32),
          // Current Password
          Text(
            'Current Password',
            style: TextStyle(
              color: AppTheme.primaryText,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 8),
          Container(
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.5),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.accentColor.withValues(alpha: 0.3),
              ),
            ),
            child: TextFormField(
              controller: _currentPasswordController,
              obscureText: true,
              cursorColor: AppTheme.accentColor,
              style: TextStyle(color: AppTheme.primaryText),
              decoration: InputDecoration(
                hintText: 'Enter current password',
                hintStyle: TextStyle(color: AppTheme.secondaryText),
                border: InputBorder.none,
                contentPadding: const EdgeInsets.all(12),
              ),
            ),
          ),
          const SizedBox(height: 16),

          // New Password
          Text(
            'New Password',
            style: TextStyle(
              color: AppTheme.primaryText,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 8),
          Container(
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.5),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.accentColor.withValues(alpha: 0.3),
              ),
            ),
            child: TextFormField(
              controller: _newPasswordController,
              obscureText: true,
              cursorColor: AppTheme.accentColor,
              style: TextStyle(color: AppTheme.primaryText),
              decoration: InputDecoration(
                hintText: 'Enter new password',
                hintStyle: TextStyle(color: AppTheme.secondaryText),
                border: InputBorder.none,
                contentPadding: const EdgeInsets.all(12),
              ),
            ),
          ),
          const SizedBox(height: 16),

          // Confirm Password
          Text(
            'Confirm New Password',
            style: TextStyle(
              color: AppTheme.primaryText,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 8),
          Container(
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.5),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.accentColor.withValues(alpha: 0.3),
              ),
            ),
            child: TextFormField(
              controller: _confirmPasswordController,
              obscureText: true,
              cursorColor: AppTheme.accentColor,
              style: TextStyle(color: AppTheme.primaryText),
              decoration: InputDecoration(
                hintText: 'Confirm new password',
                hintStyle: TextStyle(color: AppTheme.secondaryText),
                border: InputBorder.none,
                contentPadding: const EdgeInsets.all(12),
              ),
            ),
          ),
          const SizedBox(height: 24),

          // Change Password Button
          Center(
            child: SizedBox(
              width: 180,
              child: ModernGlassButton(
                onPressed: _changePassword,
                child: const Text('Change Password'),
              ),
            ),
          ),
          const SizedBox(height: 48),

          // Danger Zone
          Container(
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              color: AppTheme.errorColor.withValues(alpha: 0.05),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.errorColor.withValues(alpha: 0.3),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Icon(
                      Icons.warning_amber_rounded,
                      color: AppTheme.errorColor,
                      size: 24,
                    ),
                    const SizedBox(width: 12),
                    Text(
                      'Delete My Data',
                      style: TextStyle(
                        color: AppTheme.errorColor,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 12),
                Text(
                  'Once you delete your account, there is no going back. This will permanently delete your account and remove all your data from our servers.',
                  style: TextStyle(
                    color: AppTheme.secondaryText,
                    fontSize: 14,
                    height: 1.4,
                  ),
                ),
                const SizedBox(height: 20),
                Center(
                  child: SizedBox(
                    width: 160,
                    child: ElevatedButton(
                      onPressed: _showDeleteAccountConfirmation,
                      style: ButtonStyle(
                        splashFactory: NoSplash.splashFactory,
                        overlayColor: WidgetStateProperty.resolveWith<Color?>((
                          Set<WidgetState> states,
                        ) {
                          if (states.contains(WidgetState.pressed)) {
                            return Colors.transparent;
                          }
                          return null;
                        }),
                        backgroundColor: WidgetStateProperty.all(
                          AppTheme.errorColor,
                        ),
                        foregroundColor: WidgetStateProperty.all(Colors.white),
                        elevation: WidgetStateProperty.all(0),
                        shadowColor: WidgetStateProperty.all(
                          Colors.transparent,
                        ),
                        padding: WidgetStateProperty.all(
                          const EdgeInsets.symmetric(vertical: 12),
                        ),
                        shape: WidgetStateProperty.all(
                          RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                      ),
                      child: const Text(
                        'Delete Account',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSubscriptionTab() {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Subscription Status Card
          Container(
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.5),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.accentColor.withValues(alpha: 0.3),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Icon(Icons.star, color: AppTheme.accentColor, size: 24),
                    const SizedBox(width: 12),
                    Text(
                      'Current Plan',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Text(
                  'Free Trial',
                  style: TextStyle(
                    color: AppTheme.accentColor,
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  '14 days free, perfect for getting started',
                  style: TextStyle(color: AppTheme.secondaryText, fontSize: 14),
                ),
                const SizedBox(height: 16),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      '5 GB Storage',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Core Features',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          const SizedBox(height: 24),

          // Upgrade Section
          Text(
            'Upgrade Options',
            style: TextStyle(
              color: AppTheme.primaryText,
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),

          // Basic Plan Card
          Container(
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.3),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.secondaryText.withValues(alpha: 0.2),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Icon(
                      Icons.cloud_queue,
                      color: AppTheme.accentColor,
                      size: 24,
                    ),
                    const SizedBox(width: 12),
                    Text(
                      'Basic Plan',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Spacer(),
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 6,
                      ),
                      decoration: BoxDecoration(
                        color: AppTheme.accentColor.withValues(alpha: 0.1),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Text(
                        '€4.99/month',
                        style: TextStyle(
                          color: AppTheme.accentColor,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Text(
                  'Essential features for individuals and small teams',
                  style: TextStyle(color: AppTheme.secondaryText, fontSize: 14),
                ),
                const SizedBox(height: 16),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      '10 GB Storage',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      '1 Organization',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'File Sharing & Viewing',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Collabora Document Editing',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Share Links',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 12),
                Text(
                  'Student discount: €3.49/month',
                  style: TextStyle(
                    color: AppTheme.secondaryText,
                    fontSize: 12,
                    fontStyle: FontStyle.italic,
                  ),
                ),
                const SizedBox(height: 16),
                Center(
                  child: SizedBox(
                    width: 120,
                    child: ModernGlassButton(
                      onPressed: () {
                        // TODO: Implement upgrade functionality
                      },
                      child: const Text('Upgrade'),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 16),

          // Pro Plan Card
          Container(
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.3),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.secondaryText.withValues(alpha: 0.2),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Icon(
                      Icons.workspace_premium,
                      color: AppTheme.accentColor,
                      size: 24,
                    ),
                    const SizedBox(width: 12),
                    Text(
                      'Pro Plan',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Spacer(),
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 6,
                      ),
                      decoration: BoxDecoration(
                        color: AppTheme.accentColor.withValues(alpha: 0.1),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Text(
                        '€9.99/month',
                        style: TextStyle(
                          color: AppTheme.accentColor,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Text(
                  'Advanced features for growing teams and businesses',
                  style: TextStyle(color: AppTheme.secondaryText, fontSize: 14),
                ),
                const SizedBox(height: 16),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      '100 GB Storage',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Unlimited Organizations',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Advanced Team Features',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Audit Logs',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Priority Support',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Center(
                  child: SizedBox(
                    width: 120,
                    child: ModernGlassButton(
                      onPressed: () {
                        // TODO: Implement upgrade functionality
                      },
                      child: const Text('Upgrade'),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 16),

          // Enterprise Plan Card
          Container(
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  AppTheme.accentColor.withValues(alpha: 0.1),
                  AppTheme.primaryBackground.withValues(alpha: 0.3),
                ],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.accentColor.withValues(alpha: 0.4),
              ),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Icon(Icons.business, color: AppTheme.accentColor, size: 24),
                    const SizedBox(width: 12),
                    Text(
                      'Enterprise Plan',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Spacer(),
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 6,
                      ),
                      decoration: BoxDecoration(
                        color: AppTheme.accentColor.withValues(alpha: 0.2),
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Text(
                        '€19.99/month',
                        style: TextStyle(
                          color: AppTheme.accentColor,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Text(
                  'Complete solution for large organizations and enterprises',
                  style: TextStyle(color: AppTheme.secondaryText, fontSize: 14),
                ),
                const SizedBox(height: 8),
                Text(
                  '€19.99/month for first 20 users • €9.99/month per additional user',
                  style: TextStyle(
                    color: AppTheme.secondaryText,
                    fontSize: 12,
                    fontStyle: FontStyle.italic,
                  ),
                ),
                const SizedBox(height: 16),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Unlimited Storage',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Unlimited Organizations',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Premium Support',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Icon(
                      Icons.check_circle,
                      color: AppTheme.accentColor,
                      size: 16,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      'Custom Integrations',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                Center(
                  child: SizedBox(
                    width: 140,
                    child: ModernGlassButton(
                      onPressed: () {
                        // TODO: Implement enterprise upgrade functionality
                      },
                      child: const Text('Get Started'),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 24),

          // Usage Stats
          Text(
            'Usage Statistics',
            style: TextStyle(
              color: AppTheme.primaryText,
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          Container(
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              color: AppTheme.primaryBackground.withValues(alpha: 0.3),
              borderRadius: BorderRadius.circular(16),
              border: Border.all(
                color: AppTheme.secondaryText.withValues(alpha: 0.2),
              ),
            ),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'Storage Used',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                    Text(
                      '2.3 GB / 5 GB',
                      style: TextStyle(
                        color: AppTheme.secondaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 12),
                LinearProgressIndicator(
                  value: 0.46, // 2.3GB / 5GB
                  backgroundColor: AppTheme.secondaryText.withValues(
                    alpha: 0.2,
                  ),
                  valueColor: AlwaysStoppedAnimation<Color>(
                    AppTheme.accentColor,
                  ),
                ),
                const SizedBox(height: 16),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'Files Shared',
                      style: TextStyle(
                        color: AppTheme.primaryText,
                        fontSize: 14,
                      ),
                    ),
                    Text(
                      '47 files',
                      style: TextStyle(
                        color: AppTheme.secondaryText,
                        fontSize: 14,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
