import 'package:em2rp/providers/local_user_provider.dart';
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:em2rp/models/user_model.dart';
import 'package:em2rp/views/widgets/inputs/styled_text_field.dart';
import 'package:em2rp/views/widgets/image/profile_picture_selector.dart';
import 'package:em2rp/views/widgets/nav/custom_app_bar.dart';
import 'package:em2rp/views/widgets/notification_preferences_widget.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const CustomAppBar(
        title: 'Mon compte',
      ),
      drawer: const MainDrawer(currentPage: '/my_account'),
      body: Selector<LocalUserProvider, UserModel?>(
        selector: (context, provider) => provider.currentUser,
        builder: (context, user, child) {
          if (user == null) {
            return const Center(child: CircularProgressIndicator());
          }

          final firstNameController =
              TextEditingController(text: user.firstName);
          final lastNameController = TextEditingController(text: user.lastName);
          final phoneController = TextEditingController(text: user.phoneNumber);

          return SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const ProfilePictureSelector(),
                  Center(
                    child: Card(
                      elevation: 4.0,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12.0),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(24.0),
                        child: ConstrainedBox(
                          constraints: const BoxConstraints(maxWidth: 500),
                          child: Column(
                            children: [
                              StyledTextField(
                                labelText: 'Prénom',
                                controller: firstNameController,
                              ),
                              const SizedBox(height: 16),
                              StyledTextField(
                                labelText: 'Nom',
                                controller: lastNameController,
                              ),
                              const SizedBox(height: 16),
                              StyledTextField(
                                labelText: 'Numéro de téléphone',
                                controller: phoneController,
                              ),
                              const SizedBox(height: 16),
                              StyledTextField(
                                labelText: 'Email',
                                controller:
                                    TextEditingController(text: user.email),
                                enabled: false,
                              ),
                              const SizedBox(height: 20),
                              ElevatedButton(
                                onPressed: () {
                                  context.read<LocalUserProvider>().updateUserData(
                                    firstName: firstNameController.text,
                                    lastName: lastNameController.text,
                                    phoneNumber: phoneController.text,
                                  );
                                },
                                child: const Text('Enregistrer'),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: 24),

                  // Section Préférences de notifications
                  ConstrainedBox(
                    constraints: const BoxConstraints(maxWidth: 600),
                    child: const NotificationPreferencesWidget(),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
