// lib/ui/impostazioni/impostazioni_view.dart
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/routes/routes.dart';
import 'package:flux/core/theme/theme.dart';
import 'package:flux/features/settings/blocs/settings_cubit.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';

class SettingsScreen extends StatelessWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Impostazioni')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          _settingsSection('Utente', [
            _settingsTile(
              title: 'Impostazioni Promemoria',
              icon: Icons.notifications,
              subtitle: 'Notifiche predefinite',
              context: context,
              onTap: () => context.pushNamed(Routes.reminderSettings),
            ),
          ]),
          _settingsSection('Azienda', [
            _settingsTile(
              title: 'Impostazioni Azienda',
              icon: Icons.business,
              subtitle: 'Configura i dati aziendali',
              context: context,
              onTap: () => context.pushNamed(Routes.companySettings),
            ),
            const Divider(height: 30),
            _settingsTile(
              title: 'Impostazione Negozi',
              icon: Icons.store,
              subtitle: 'Crea o configura i negozi',
              context: context,
              onTap: () => context.pushNamed(Routes.stores),
            ),
            const Divider(height: 30),
            _settingsTile(
              title: 'Impostazione Staff / Utenti',
              icon: Icons.group,
              subtitle:
                  'Configura i membri dei negozi o invita nuovi utenti in azienda',
              context: context,
              onTap: () => context.pushNamed(Routes.staff),
            ),
          ]),
          const SizedBox(height: 20),
          _settingsSection('Applicazione', [
            BlocBuilder<SettingsCubit, SettingsState>(
              builder: (context, state) => CheckboxListTile(
                value: state.isSingleUserMode,

                title: Row(
                  children: [
                    const Icon(Icons.person, color: FluxColors.primaryBlue),
                    const SizedBox(width: 12),
                    Flexible(
                      child: Text(
                        'Modalità utente singolo (dispositivo personale)',
                        style: Theme.of(context).textTheme.titleLarge,
                      ),
                    ),
                  ],
                ),
                subtitle: Padding(
                  padding: const EdgeInsets.only(left: 36),
                  child: Text(
                    'Utente ${GetIt.I.get<SessionCubit>().state.currentStaffMember?.name ?? 'Nessuno'} selezionato automaticamente',
                  ),
                ),
                onChanged: (value) {
                  context.read<SessionCubit>().setIsSingleUserMode(value!);
                  context.read<SettingsCubit>().toggleSingleUserMode();
                },
              ),
            ),
            const Divider(height: 30),
            _settingsTile(
              icon: Icons.dark_mode,
              title: 'Tema (FLUX Dark)',
              subtitle: 'Configurazione visiva',
              context: context,
              onTap: () => context.pushNamed(Routes.themeSettings),
            ),
          ]),

          const SizedBox(height: 24),
          TextButton.icon(
            onPressed: () => context.read<SessionCubit>().signOut(),
            icon: const Icon(Icons.exit_to_app, color: Colors.red),
            label: const Text('Logout', style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );
  }

  Widget _settingsSection(String title, List<Widget> tiles) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title.toUpperCase(),
          style: const TextStyle(
            color: FluxColors.accentTurquoise,
            fontWeight: FontWeight.bold,
            letterSpacing: 1,
          ),
        ),
        const SizedBox(height: 8),
        Card(child: Column(children: tiles)),
      ],
    );
  }

  Widget _settingsTile({
    required BuildContext context,
    required IconData icon,
    required String title,
    String? subtitle,
    required VoidCallback onTap,
  }) {
    return ListTile(
      leading: Icon(icon, color: FluxColors.primaryBlue),
      title: Text(title, style: Theme.of(context).textTheme.titleLarge),
      subtitle: Text(subtitle ?? ''),
      trailing: Icon(
        Icons.chevron_right,
        color: Theme.of(context).textTheme.bodyMedium?.color,
      ),
      onTap: onTap,
    );
  }
}
