import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/chat/active_session_notifier.dart';
import '../../../core/theme/halo_tokens.dart';
import '../widgets/chat_row.dart';

/// Chat-Tab > aktif sub-tab.
///
/// Always renders 0 or 1 row — backend caps the customer to one active
/// session. Row stays visible while the user is inside the live chat room
/// (per §10.6 decision 1: aktif represents state, not navigation).
class AktifView extends ConsumerWidget {
  const AktifView({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final async = ref.watch(activeSessionProvider);
    return RefreshIndicator(
      onRefresh: () => ref.read(activeSessionProvider.notifier).refresh(),
      color: HaloTokens.brand,
      child: async.when(
        loading: () => const _Loading(),
        error: (e, _) => _Error(message: e.toString()),
        data: (data) {
          if (!data.hasSession) return const _Empty();
          final session = data.session!;
          final mitraName =
              (session['mitra_display_name'] as String?) ?? 'Bestie';
          return ListView(
            padding: const EdgeInsets.symmetric(
              horizontal: HaloSpacing.s20,
              vertical: HaloSpacing.s12,
            ),
            children: [
              ChatRow(
                seed: mitraName.codeUnits.fold<int>(0, (a, b) => a + b),
                name: mitraName,
                preview: _previewFor(data.unreadCount),
                isLive: true,
                isCall: (session['mode'] as String?) == 'call',
                onTap: () {
                  final sessionId = session['id'] as String?;
                  if (sessionId == null) return;
                  context.push('/chat/session/$sessionId',
                      extra: {'mitraName': mitraName});
                },
              ),
            ],
          );
        },
      ),
    );
  }

  String _previewFor(int unread) =>
      unread > 0 ? '$unread pesan baru' : 'lagi ngobrol nih';
}

class _Empty extends StatelessWidget {
  const _Empty();
  @override
  Widget build(BuildContext context) => const _CenteredMessage(
        text: 'belum ada chat di sini',
      );
}

class _Loading extends StatelessWidget {
  const _Loading();
  @override
  Widget build(BuildContext context) => const Center(
        child: CircularProgressIndicator(color: HaloTokens.brand),
      );
}

class _Error extends StatelessWidget {
  final String message;
  const _Error({required this.message});
  @override
  Widget build(BuildContext context) =>
      _CenteredMessage(text: 'gagal memuat: $message');
}

class _CenteredMessage extends StatelessWidget {
  final String text;
  const _CenteredMessage({required this.text});
  @override
  Widget build(BuildContext context) {
    // Wrap in a scroll view so RefreshIndicator works even on empty state.
    return ListView(
      physics: const AlwaysScrollableScrollPhysics(),
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(
            horizontal: HaloSpacing.s20,
            vertical: HaloSpacing.s64,
          ),
          child: Text(
            text,
            textAlign: TextAlign.center,
            style: const TextStyle(
              fontFamily: HaloTokens.fontBody,
              fontSize: 13,
              color: HaloTokens.inkMuted,
            ),
          ),
        ),
      ],
    );
  }
}
