import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/invitation/invitation.dart';
import 'package:pshared/provider/invitations.dart';

import 'package:pweb/utils/error/snackbar.dart';

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


class InvitationCardActions extends StatelessWidget {
  final Invitation invitation;

  const InvitationCardActions({super.key, required this.invitation});

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        if (invitation.isPending && !invitation.isExpired)
          TextButton.icon(
            onPressed: () => _revokeInvitation(context),
            icon: const Icon(Icons.block),
            label: Text(loc.invitationRevokeAction),
          ),
        if (!invitation.isArchived)
          TextButton.icon(
            onPressed: () => _archiveInvitation(context),
            icon: const Icon(Icons.archive_outlined),
            label: Text(loc.invitationArchiveAction),
          ),
      ],
    );
  }

  Future<void> _archiveInvitation(BuildContext context) async {
    final loc = AppLocalizations.of(context)!;
    final provider = context.read<InvitationsProvider>();

    await executeActionWithNotification(
      context: context,
      action: () => provider.setInvitationArchived(invitation, true),
      successMessage: loc.invitationArchived,
      errorMessage: loc.invitationArchiveFailed,
    );
  }

  Future<void> _revokeInvitation(BuildContext context) async {
    final loc = AppLocalizations.of(context)!;
    final provider = context.read<InvitationsProvider>();

    await executeActionWithNotification(
      context: context,
      action: () => provider.revokeInvitation(invitation),
      successMessage: loc.invitationRevoked,
      errorMessage: loc.invitationRevokeFailed,
    );
  }
}
