import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:real_estate_mobile/core/constants/app_colors.dart';
import 'package:real_estate_mobile/features/home/data/models/landing_page_content.dart';
import 'package:real_estate_mobile/features/home/presentation/providers/home_provider.dart';

/// Default testimonials — matches web's hardcoded defaultTestimonials fallback.
const _defaultTestimonials = [
  TestimonialItem(
    rating: 5,
    title: 'I have been working with Lorem ..',
    content:
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    author: 'Conor Kenney',
    role: 'Chief Operations Officer',
  ),
  TestimonialItem(
    rating: 5,
    title: 'I have been working with Lorem ..',
    content:
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    author: 'Conor Kenney',
    role: 'Chief Operations Officer',
  ),
  TestimonialItem(
    rating: 5,
    title: 'I have been working with Lorem ..',
    content:
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    author: 'Conor Kenney',
    role: 'Chief Operations Officer',
  ),
];

/// Default section content — matches web's hardcoded defaultContent fallback.
const _defaultContent = TestimonialsContent(
  title: "Our Clients' Trust Is Our Foundation",
  subtitle:
      'We help buyers, sellers, and investors close successful real estate deals with confidence in every transaction.',
  ratingInfo:
      'Clients rate our real estate services 4.9 out of 5 on average, based on recent client reviews.',
  stats: [
    StatItem(
      iconPath: 'cities',
      boldText: '25+ Cities &',
      normalText: 'neighborhoods served',
    ),
    StatItem(
      iconPath: 'properties',
      boldText: '1,000+ Properties',
      normalText: 'bought and sold for our clients.',
    ),
  ],
  testimonials: _defaultTestimonials,
);

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

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final homeState = ref.watch(homeProvider);
    final cmsTestimonials = homeState.content?.testimonials;

    // Use CMS data if available with non-empty testimonials, otherwise fallback
    final data =
        (cmsTestimonials != null && cmsTestimonials.testimonials.isNotEmpty)
            ? cmsTestimonials
            : _defaultContent;

    final testimonials =
        data.testimonials.isNotEmpty ? data.testimonials : _defaultTestimonials;

    return Column(
      children: [
        // Testimonial cards carousel (header is in TrustStatsSection above)
        SizedBox(
          height: 420,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            padding: const EdgeInsets.symmetric(horizontal: 24),
            itemCount: testimonials.length,
            itemBuilder: (context, index) => Padding(
              padding: const EdgeInsets.only(right: 16),
              child: _buildTestimonialCard(testimonials[index]),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildTestimonialCard(TestimonialItem testimonial) {
    return Container(
      width: 300,
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(15),
        border: Border.all(
          color: AppColors.primaryDark.withValues(alpha: 0.1),
          width: 1,
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Quote icon SVG
          SvgPicture.asset(
            'assets/icons/quote_icon.svg',
            width: 23,
            height: 13,
          ),
          const SizedBox(height: 12),
          // Title
          Text(
            testimonial.title,
            style: const TextStyle(
              fontFamily: 'Fractul',
              fontSize: 14,
              fontWeight: FontWeight.w700,
              color: AppColors.primaryDark,
            ),
          ),
          const SizedBox(height: 12),
          // Content
          Expanded(
            child: Text(
              testimonial.content,
              style: const TextStyle(
                fontFamily: 'SourceSerif4',
                fontSize: 14,
                fontWeight: FontWeight.w400,
                color: AppColors.primaryDark,
              ),
            ),
          ),
          const SizedBox(height: 16),
          // Stars (yellow like Figma)
          Row(
            children: List.generate(
              testimonial.rating,
              (index) => const Padding(
                padding: EdgeInsets.only(right: 4),
                child: Icon(
                  Icons.star,
                  color: Color(0xFFFFDE21),
                  size: 18,
                ),
              ),
            ),
          ),
          const SizedBox(height: 12),
          // Author name
          Text(
            testimonial.author,
            style: const TextStyle(
              fontFamily: 'SourceSerif4',
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: AppColors.primaryDark,
            ),
          ),
          const SizedBox(height: 2),
          // Role
          Text(
            testimonial.role,
            style: const TextStyle(
              fontFamily: 'SourceSerif4',
              fontSize: 14,
              fontWeight: FontWeight.w600,
              color: AppColors.primaryDark,
            ),
          ),
        ],
      ),
    );
  }
}
