import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/routes/routes.dart';
import 'package:flux/core/widgets/staff_selector_modal.dart';
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
import 'package:flux/features/tickets/blocs/ticket_list_cubit.dart';
import 'package:flux/features/tickets/blocs/ticket_list_state.dart';
import 'package:flux/features/tickets/models/ticket_model.dart';
import 'package:flux/features/tickets/models/ticket_status_extension.dart';
import 'package:flux/features/tickets/ui/widgets/ticket_list.dart';
import 'package:go_router/go_router.dart';

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

  @override
  State<TicketListScreen> createState() => _TicketListScreenState();
}

class _TicketListScreenState extends State<TicketListScreen> {
  final ScrollController _scrollController = ScrollController();
  final TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    // INFINITY SCROLL: Quando arriviamo quasi in fondo, chiediamo altri ticket
    _scrollController.addListener(() {
      if (_scrollController.position.pixels >=
          _scrollController.position.maxScrollExtent - 200) {
        context.read<TicketListCubit>().loadTickets();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    _searchController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Assistenza & Riparazioni'),
        actions: [
          IconButton(
            onPressed: () =>
                context.read<TicketListCubit>().loadTickets(refresh: true),
            icon: const Icon(Icons.refresh),
          ),
          // Tasto per filtri avanzati (Data, Staff, Tipo) -> Da fare in un BottomSheet!
          IconButton(
            icon: const Icon(Icons.filter_list),
            onPressed: () {
              // TODO: Aprire BottomSheet filtri avanzati
            },
          ),
        ],
      ),
      body: Column(
        children: [
          // 1. BARRA DI RICERCA
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              controller: _searchController,
              decoration: InputDecoration(
                hintText: 'Cerca per nome cliente...',
                prefixIcon: const Icon(Icons.search),
                suffixIcon: IconButton(
                  icon: const Icon(Icons.clear),
                  onPressed: () {
                    _searchController.clear();
                    context.read<TicketListCubit>().updateFilters(
                      clearSearch: true,
                    );
                  },
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              onSubmitted: (value) {
                context.read<TicketListCubit>().updateFilters(
                  searchTerm: value,
                );
              },
            ),
          ),

          // 2. FILTRI RAPIDI PER STATO (CHIPS)
          BlocBuilder<TicketListCubit, TicketListState>(
            buildWhen: (previous, current) =>
                previous.statusFilter != current.statusFilter,
            builder: (context, state) {
              return SizedBox(
                height: 50,
                child: ListView(
                  scrollDirection: Axis.horizontal,
                  padding: const EdgeInsets.symmetric(horizontal: 8.0),
                  children: [
                    _buildStatusChip(context, state, null, 'Tutti'),
                    ...TicketStatus.values.map(
                      (status) => _buildStatusChip(
                        context,
                        state,
                        status,
                        status.displayValue,
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
          const Divider(),

          // 3. LA LISTA DEI TICKET
          Expanded(
            child: BlocBuilder<TicketListCubit, TicketListState>(
              builder: (context, state) {
                if (state.isLoading && state.tickets.isEmpty) {
                  return const Center(child: CircularProgressIndicator());
                }

                if (state.tickets.isEmpty) {
                  return const Center(child: Text('Nessun ticket trovato.'));
                }

                return TicketList(
                  state: state,
                  scrollController: _scrollController,
                );
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: const Icon(Icons.add),
        label: const Text('Nuovo Ticket'),
        onPressed: () async {
          StaffMemberModel? createdBy = await getStaffMember(context);
          if (createdBy == null || !context.mounted) return;
          await context.pushNamed(
            Routes.ticketForm,
            pathParameters: {'id': 'new'},
            extra: (createdBy: createdBy, ticket: null),
          );
          if (!context.mounted) return;
          context.read<TicketListCubit>().loadTickets(refresh: true);
        },
      ),
    );
  }

  // Widget di supporto per creare le Chip di filtro
  Widget _buildStatusChip(
    BuildContext context,
    TicketListState state,
    TicketStatus? status,
    String label,
  ) {
    final isSelected = state.statusFilter == status;
    return Padding(
      padding: const EdgeInsets.only(right: 8.0),
      child: ChoiceChip(
        label: Text(label),
        selected: isSelected,
        selectedColor:
            status?.color.withValues(alpha: 0.2) ??
            Colors.blue.withValues(alpha: 0.2),
        onSelected: (selected) {
          context.read<TicketListCubit>().updateFilters(
            statusFilter: selected ? status : null,
            clearStatus: !selected && status != null,
          );
        },
      ),
    );
  }
}
