import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
import 'package:flux/core/utils/validators.dart';
import 'package:flux/core/widgets/flux_text_field.dart';
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
import 'package:flux/features/onboarding/blocs/onboarding_cubit.dart';
import 'package:get_it/get_it.dart';

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

  @override
  State<StaffOnboardingForm> createState() => _StaffOnboardingFormState();
}

class _StaffOnboardingFormState extends State<StaffOnboardingForm> {
  final _formKey = GlobalKey<FormState>();
  final _nameCtrl = TextEditingController();
  final _emailCtrl = TextEditingController();
  final _jobTitleCtrl = TextEditingController();

  @override
  void dispose() {
    _nameCtrl.dispose();
    _emailCtrl.dispose();
    _jobTitleCtrl.dispose();
    super.dispose();
  }

  @override
  void initState() {
    _emailCtrl.text = GetIt.I.get<SessionCubit>().state.user?.email ?? '';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(32),
      child: Form(
        key: _formKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              "Il tuo Profilo 👤",
              style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            const Text(
              "Ultimo step! Crea il tuo profilo operativo per iniziare a usare FLUX.",
              style: TextStyle(fontSize: 16, color: Colors.grey),
            ),
            const SizedBox(height: 48),
            FluxTextField(
              label: 'Nome',
              keyboardType: TextInputType.name,
              controller: _nameCtrl,
              validator: notEmptyValidator,
              textCapitalization: TextCapitalization.words,
              autocorrect: false,
            ),
            const SizedBox(height: 16),
            FluxTextField(
              label: 'Email',
              keyboardType: TextInputType.emailAddress,
              controller: _emailCtrl,
              textCapitalization: TextCapitalization.none,
            ),
            const SizedBox(height: 16),
            FluxTextField(
              label: 'Etichetta Ruolo (es. Titolare, Manager)',
              controller: _jobTitleCtrl,
              keyboardType: TextInputType.text,
              textCapitalization: TextCapitalization.words,
              onSubmitted: (_) => _submit(),
            ),
            const Spacer(),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 16),
                backgroundColor: Colors.black, // O il tuo context.accent
                foregroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              onPressed: () => _submit(),
              child: const Text(
                "Entra in FLUX",
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
            ),
            const SizedBox(height: 16),
          ],
        ),
      ),
    );
  }

  void _submit() {
    if (_formKey.currentState!.validate()) {
      final newStaff = StaffMemberModel.empty().copyWith(
        name: _nameCtrl.text.trim(),
        email: _emailCtrl.text.trim(),
        jobTitle: _jobTitleCtrl.text.trim(),
      );
      context.read<OnboardingCubit>().saveStaff(newStaff);
    }
  }
}
