import 'package:finnow_app/api/auth.dart';
import 'package:finnow_app/auth/model.dart';
import 'package:finnow_app/components/form_label.dart';
import 'package:flutter/material.dart';

import '../main.dart';

class UserAccountPage extends StatelessWidget {
  final Authenticated auth;
  const UserAccountPage(this.auth, {super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.all(10),
        child: ListView(children: [
          const Text('My User',
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 32.0)),
          const SizedBox(height: 10),
          const FormLabel('Username'),
          Text(auth.username),
          const SizedBox(height: 10),
          Row(children: [
            FilledButton(
                onPressed: () => attemptDeleteUser(context),
                child: const Text('Delete my user'))
          ])
        ]));
  }

  void attemptDeleteUser(BuildContext ctx) async {
    bool confirmed = false;
    await showDialog(
        context: ctx,
        builder: (context) {
          return AlertDialog(
              title: const Text('Confirm User Deletion'),
              content: const Text(
                  'Are you sure you want to delete your user? This is a permanent action that cannot be undone. All your profiles will be deleted.'),
              actions: [
                FilledButton(
                    onPressed: () {
                      confirmed = true;
                      Navigator.of(context).pop();
                    },
                    child: const Text('Ok')),
                FilledButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: const Text('Cancel'))
              ]);
        });
    if (confirmed) {
      await deleteUser(auth.token);
      getIt<AuthenticationModel>().state = Unauthenticated();
    }
  }
}
