import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:real_estate_mobile/core/constants/app_colors.dart';
import 'package:real_estate_mobile/features/home/presentation/widgets/features_section.dart';
import 'package:real_estate_mobile/features/home/presentation/widgets/hero_section.dart';
import 'package:real_estate_mobile/features/home/presentation/widgets/testimonials_section.dart';
import 'package:real_estate_mobile/features/home/presentation/widgets/top_professionals_section.dart';
import 'package:real_estate_mobile/features/home/presentation/widgets/trust_stats_section.dart';

/// Global scroll controller for the home screen so the bottom nav can scroll to top.
final homeScrollControllerProvider = Provider<ScrollController>((ref) {
  final controller = ScrollController();
  ref.onDispose(() => controller.dispose());
  return controller;
});

class HomeScreen extends ConsumerWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final scrollController = ref.watch(homeScrollControllerProvider);
    // Use ListView.builder for lazy rendering — only builds sections as
    // they scroll into view, instead of rendering all 5 heavy sections at
    // once in a Column (which causes jank on initial load).
    final sections = <Widget>[
      const RepaintBoundary(child: HeroSection()),
      const SizedBox(height: 40),
      const RepaintBoundary(child: FeaturesSection()),
      const SizedBox(height: 40),
      const RepaintBoundary(child: TopProfessionalsSection()),
      const SizedBox(height: 40),
      const RepaintBoundary(child: TrustStatsSection()),
      const SizedBox(height: 24),
      const RepaintBoundary(child: TestimonialsSection()),
      const SizedBox(height: 40),
    ];
    return ListView.builder(
      controller: scrollController,
      padding: EdgeInsets.zero,
      itemCount: sections.length,
      itemBuilder: (_, i) => sections[i],
    );
  }

  static const _footerRoutes = <String, String>{
    'Home': '/home',
    'About': '/about',
    'Contact': '/contact',
    'FAQ': '/faq',
  };

  Widget _buildFooter(BuildContext context, WidgetRef ref) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 24),
      color: AppColors.primaryDark,
      child: Column(
        children: [
          Image.asset(
            'assets/icons/logo.png',
            width: 109,
            height: 29,
            color: Colors.white,
          ),
          const SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildFooterLink(context, 'Home', ref),
              _buildFooterDot(),
              _buildFooterLink(context, 'About', ref),
              _buildFooterDot(),
              _buildFooterLink(context, 'Contact', ref),
              _buildFooterDot(),
              _buildFooterLink(context, 'FAQ', ref),
            ],
          ),
          const SizedBox(height: 16),
          Text(
            '\u00A9 2025 RE-Quest. All rights reserved.',
            style: TextStyle(
              fontFamily: 'SourceSerif4',
              fontSize: 12,
              fontWeight: FontWeight.w400,
              color: Colors.white.withValues(alpha: 0.6),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFooterLink(BuildContext context, String text, WidgetRef ref) {
    return GestureDetector(
      onTap: () {
        final route = _footerRoutes[text];
        if (route == null) return;
        final currentLocation = GoRouterState.of(context).uri.toString();
        if (route == '/home' && currentLocation == '/home') {
          // Already on home — scroll to top
          ref.read(homeScrollControllerProvider).animateTo(
                0,
                duration: const Duration(milliseconds: 400),
                curve: Curves.easeOut,
              );
        } else {
          context.go(route);
        }
      },
      child: Text(
        text,
        style: const TextStyle(
          fontFamily: 'SourceSerif4',
          fontSize: 14,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  Widget _buildFooterDot() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 12),
      child: Text(
        '\u2022',
        style: TextStyle(
          fontSize: 14,
          color: Colors.white.withValues(alpha: 0.5),
        ),
      ),
    );
  }
}
