import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../data/providers/friends_provider.dart';
import 'friend_request_card.dart';
import 'requests_empty_state.dart';
import 'requests_loading_state.dart';
import 'requests_section_header.dart';

/// Onglet affichant les demandes d'amitié reçues et envoyées.
class RequestsTab extends StatelessWidget {
  const RequestsTab({
    required this.onAccept,
    required this.onReject,
    required this.onCancel,
    required this.onRefresh,
    super.key,
  });

  final Future<void> Function(FriendsProvider, String) onAccept;
  final Future<void> Function(FriendsProvider, String) onReject;
  final Future<void> Function(FriendsProvider, String) onCancel;
  final VoidCallback onRefresh;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Consumer<FriendsProvider>(
      builder: (context, provider, child) {
        final isLoading =
            provider.isLoadingReceivedRequests || provider.isLoadingSentRequests;
        final hasReceived = provider.receivedRequests.isNotEmpty;
        final hasSent = provider.sentRequests.isNotEmpty;

        if (isLoading && !hasReceived && !hasSent) {
          return RequestsLoadingState(theme: theme);
        }

        if (!hasReceived && !hasSent) {
          return RefreshIndicator(
            onRefresh: () async {
              await Future.wait([
                provider.fetchReceivedRequests(),
                provider.fetchSentRequests(),
              ]);
            },
            child: SingleChildScrollView(
              physics: const AlwaysScrollableScrollPhysics(),
              child: SizedBox(
                height: MediaQuery.of(context).size.height - 300,
                child: RequestsEmptyState(theme: theme),
              ),
            ),
          );
        }

        return _buildRequestsList(theme, provider);
      },
    );
  }

  /// Construit la liste des demandes.
  Widget _buildRequestsList(ThemeData theme, FriendsProvider provider) {
    return RefreshIndicator(
      onRefresh: () async {
        await Future.wait([
          provider.fetchReceivedRequests(),
          provider.fetchSentRequests(),
        ]);
      },
      child: ListView(
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.symmetric(vertical: 8),
        children: [
          if (provider.receivedRequests.isNotEmpty) ...[
            RequestsSectionHeader(
              title: 'Demandes reçues',
              theme: theme,
            ),
            const SizedBox(height: 8),
            ...provider.receivedRequests.map(
              (request) => FriendRequestCard(
                request: request,
                onAccept: () => onAccept(provider, request.friendshipId),
                onReject: () => onReject(provider, request.friendshipId),
              ),
            ),
            const SizedBox(height: 24),
          ],
          if (provider.sentRequests.isNotEmpty) ...[
            RequestsSectionHeader(
              title: 'Demandes envoyées',
              theme: theme,
            ),
            const SizedBox(height: 8),
            ...provider.sentRequests.map(
              (request) => FriendRequestCard(
                request: request,
                onAccept: null,
                onReject: () => onCancel(provider, request.friendshipId),
                isSentRequest: true,
              ),
            ),
            const SizedBox(height: 16),
          ],
        ],
      ),
    );
  }
}

