import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/permissions.dart';

import 'package:pweb/pages/invitations/widgets/form/actions.dart';
import 'package:pweb/pages/invitations/widgets/form/fields.dart';

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


class InvitationFormView extends StatelessWidget {
  final GlobalKey<FormState> formKey;
  final TextEditingController emailController;
  final TextEditingController firstNameController;
  final TextEditingController lastNameController;
  final TextEditingController messageController;
  final int expiryDays;
  final ValueChanged<int> onExpiryChanged;
  final String? selectedRoleRef;
  final ValueChanged<String?> onRoleChanged;
  final bool canCreate;
  final VoidCallback onSubmit;

  const InvitationFormView({
    super.key,
    required this.formKey,
    required this.emailController,
    required this.firstNameController,
    required this.lastNameController,
    required this.messageController,
    required this.expiryDays,
    required this.onExpiryChanged,
    required this.selectedRoleRef,
    required this.onRoleChanged,
    required this.canCreate,
    required this.onSubmit,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final loc = AppLocalizations.of(context)!;
    final roles = context.watch<PermissionsProvider>().roleDescriptions;

    return Card(
      elevation: 0,
      color: theme.colorScheme.surfaceContainerHighest.withAlpha(40),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Form(
          key: formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                loc.invitationCreateTitle,
                style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600),
              ),
              const SizedBox(height: 12),
              InvitationFormFields(
                roles: roles,
                emailController: emailController,
                firstNameController: firstNameController,
                lastNameController: lastNameController,
                messageController: messageController,
                selectedRoleRef: selectedRoleRef,
                onRoleChanged: onRoleChanged,
              ),
              const SizedBox(height: 12),
              InvitationFormActions(
                expiryDays: expiryDays,
                onExpiryChanged: onExpiryChanged,
                canCreate: canCreate,
                hasRoles: roles.isNotEmpty,
                onSubmit: onSubmit,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
