import 'package:flutter/material.dart';

import 'package:collection/collection.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/resources.dart';
import 'package:pshared/provider/account.dart';
import 'package:pshared/provider/invitations.dart';
import 'package:pshared/provider/permissions.dart';

import 'package:pweb/pages/invitations/widgets/header.dart';
import 'package:pweb/pages/invitations/widgets/form/form.dart';
import 'package:pweb/pages/invitations/widgets/list/list.dart';
import 'package:pweb/pages/loader.dart';
import 'package:pweb/widgets/error/snackbar.dart';
import 'package:pweb/widgets/roles/create_role_dialog.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


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

  @override
  State<InvitationsPage> createState() => _InvitationsPageState();
}

class _InvitationsPageState extends State<InvitationsPage> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _firstNameController = TextEditingController();
  final TextEditingController _lastNameController = TextEditingController();
  final TextEditingController _messageController = TextEditingController();

  String? _selectedRoleRef;
  int _expiryDays = 7;

  Future<void> _createRole() async {
    final loc = AppLocalizations.of(context)!;
    final draft = await showCreateRoleDialog(context);
    if (draft == null) return;

    final permissions = context.read<PermissionsProvider>();
    final createdRole = await executeActionWithNotification(
      context: context,
      action: () => permissions.createRoleDescription(
        name: draft.name,
        description: draft.description.isEmpty ? null : draft.description,
      ),
      successMessage: loc.invitationRoleCreated,
      errorMessage: loc.invitationRoleCreateFailed,
    );

    if (createdRole != null && mounted) {
      setState(() => _selectedRoleRef = createdRole.id);
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _bootstrapRoleSelection();
  }

  void _bootstrapRoleSelection() {
    final roles = context.read<PermissionsProvider>().roleDescriptions;
    if (roles.isEmpty) return;
    final firstRoleRef = roles.first.storable.id;
    final isSelectedAvailable = _selectedRoleRef != null
        && roles.any((role) => role.storable.id == _selectedRoleRef);
    if (isSelectedAvailable) return;
    if (!mounted) return;
    setState(() => _selectedRoleRef = firstRoleRef);
  }

  @override
  void dispose() {
    _emailController.dispose();
    _firstNameController.dispose();
    _lastNameController.dispose();
    _messageController.dispose();
    super.dispose();
  }

  Future<void> _sendInvitation() async {
    final form = _formKey.currentState;
    if (form == null || !form.validate()) return;

    final account = context.read<AccountProvider>().account;
    if (account == null) return;
    final permissions = context.read<PermissionsProvider>();
    final roleRef = _selectedRoleRef ?? permissions.roleDescriptions.firstOrNull?.storable.id;
    if (roleRef == null) return;

    final invitations = context.read<InvitationsProvider>();
    final loc = AppLocalizations.of(context)!;

    await executeActionWithNotification(
      context: context,
      action: () => invitations.sendInvitation(
        email: _emailController.text.trim(),
        name: _firstNameController.text.trim(),
        lastName: _lastNameController.text.trim(),
        comment: _messageController.text.trim(),
        roleRef: roleRef,
        inviterRef: account.id,
        expiresAt: DateTime.now().toUtc().add(Duration(days: _expiryDays)),
      ),
      successMessage: loc.invitationCreatedSuccess,
      errorMessage: loc.errorCreatingInvitation,
    );

    _emailController.clear();
    _firstNameController.clear();
    _lastNameController.clear();
    _messageController.clear();
  }

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final permissions = context.watch<PermissionsProvider>();
    final canCreateRoles = permissions.canCreate(ResourceType.roles);

    if (!permissions.canRead(ResourceType.invitations)) {
      return PageViewLoader(
        child: Center(child: Text(loc.errorAccessDenied)),
      );
    }

    return PageViewLoader(
      child: SafeArea(
        child: SingleChildScrollView(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InvitationsHeader(loc: loc),
              const SizedBox(height: 16),
              InvitationsForm(
                formKey: _formKey,
                emailController: _emailController,
                firstNameController: _firstNameController,
                lastNameController: _lastNameController,
                messageController: _messageController,
                canCreateRoles: canCreateRoles,
                onCreateRole: _createRole,
                expiryDays: _expiryDays,
                onExpiryChanged: (value) => setState(() => _expiryDays = value),
                selectedRoleRef: _selectedRoleRef,
                onRoleChanged: (role) => setState(() => _selectedRoleRef = role),
                canCreate: permissions.canCreate(ResourceType.invitations),
                onSubmit: _sendInvitation,
              ),
              const SizedBox(height: 24),
              const InvitationsList(),
            ],
          ),
        ),
      ),
    );
  }
}
