import 'package:flutter/material.dart';

import 'package:intl/intl.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/invitation/invitation.dart';
import 'package:pshared/models/resources.dart';
import 'package:pshared/provider/accounts/employees.dart';
import 'package:pshared/provider/permissions.dart';

import 'package:pweb/pages/invitations/widgets/card/actions.dart';
import 'package:pweb/pages/invitations/widgets/card/details.dart';
import 'package:pweb/pages/invitations/widgets/card/header.dart';
import 'package:pweb/pages/invitations/widgets/card/helpers.dart';

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


class InvitationCardView extends StatelessWidget {
  final Invitation invitation;

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

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final theme = Theme.of(context);
    final permissions = context.watch<PermissionsProvider>();
    final employees = context.watch<EmployeesProvider>();
    final dateFormat = DateFormat.yMMMd().add_Hm();

    final statusLabel = invitationStatusLabel(invitation, loc);
    final statusColor = invitationStatusColor(theme, invitation);
    final roleLabel = invitationRoleLabel(permissions.roleDescriptions, invitation, loc);
    final inviterName = employees.getEmployee(invitation.inviterRef)?.fullName ?? loc.unknown;
    final canUpdate = permissions.canUpdate(ResourceType.invitations);

    return Card(
      elevation: 0,
      shape: RoundedRectangleBorder(
        side: BorderSide(color: theme.dividerColor.withAlpha(20)),
        borderRadius: BorderRadius.circular(12),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            InvitationCardHeader(
              invitation: invitation,
              statusLabel: statusLabel,
              statusColor: statusColor,
            ),
            const SizedBox(height: 12),
            InvitationCardDetails(
              invitation: invitation,
              roleLabel: roleLabel,
              inviterName: inviterName,
              dateFormat: dateFormat,
              loc: loc,
            ),
            if (canUpdate) ...[
              const SizedBox(height: 10),
              InvitationCardActions(invitation: invitation),
            ],
          ],
        ),
      ),
    );
  }
}
