import 'package:auto_route/auto_route.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/data/enums/state_status.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/scroll_layout_helper.dart';
import 'package:krow/core/presentation/widgets/ui_kit/dialogs/kw_dialog.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
import 'package:krow/core/presentation/widgets/ui_kit/kw_loading_overlay.dart';
import 'package:krow/features/profile/domain/bloc/personal_info_bloc.dart';
import 'package:krow/features/profile/presentation/widgets/personal_info_form.dart';

@RoutePage()
class PersonalInfoScreen extends StatelessWidget {
  const PersonalInfoScreen({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KwAppBar(
        titleText: 'Edit Profile',
      ),
      body: ScrollLayoutHelper(
        upperWidget: const PersonalInfoForm(),
        lowerWidget: BlocConsumer<PersonalInfoBloc, PersonalInfoState>(
          buildWhen: (previous, current) =>
              previous.status != current.status ||
              previous.isFilled != current.isFilled,
          listenWhen: (previous, current) => previous.status != current.status,
          listener: (context, state) {
            if (state.status == StateStatus.success) {}

            if(state.deleted){
              FirebaseAuth.instance.signOut();
              context.router.replace(const SplashRoute());
            }
          },
          builder: (context, state) {
            return Column(
              children: [
                KwLoadingOverlay(
                  shouldShowLoading: state.status == StateStatus.loading,
                  child: KwButton.primary(
                    label: 'Save edits',
                    height: 52,
                    disabled: !state.isFilled,
                    onPressed: () {
                      context
                          .read<PersonalInfoBloc>()
                          .add(SaveProfileChanges());
                    },
                  ),
                ),
                Gap(12),
                KwButton.outlinedPrimary(
                        label: 'Delete Account', onPressed: () {
                  _showDeleteAccDialog(context);
                })
                    .copyWith(color: AppColors.statusError)
              ],
            );
          },
        ),
      ),
    );
  }

  void _showDeleteAccDialog(BuildContext context) {
    KwDialog.show(
        context: context,
        icon: Assets.images.icons.alertTriangle,
        state: KwDialogState.negative,
        title: 'Are you sure you want to delete account?',
        message:
        'Deleting your account is permanent and cannot be undone. You will lose all your data, including saved preferences, history, and any content associated with your account',
        primaryButtonLabel: 'Delete Account',
        secondaryButtonLabel: 'Cancel',
        onPrimaryButtonPressed: (dialogContext) {
          Navigator.pop(dialogContext);
          BlocProvider.of<PersonalInfoBloc>(context)
              .add(DeactivateProfile());
        });
  }
}
