import 'package:flutter/material.dart';
// ignore: depend_on_referenced_packages
import 'package:intl/intl.dart' as intl;
import 'package:proxibuy/core/config/app_colors.dart';
import 'package:proxibuy/core/gen/assets.gen.dart';
import 'package:proxibuy/data/models/comment_model.dart';
import 'package:flutter_svg/flutter_svg.dart';

class CustomStarRating extends StatelessWidget {
  final double rating;
  final int starCount;
  final double size;

  const CustomStarRating({
    super.key,
    required this.rating,
    this.starCount = 5,
    this.size = 10.0,
  });

  @override
  Widget build(BuildContext context) {
    List<Widget> stars = [];
    double remaining = rating;

    for (int i = 0; i < starCount; i++) {
      if (remaining >= 1) {
        stars.add(_buildStar(Assets.icons.starFill.path));
        remaining -= 1;
      } else if (remaining >= 0.5) {
        stars.add(_buildStar(Assets.icons.star2.path));
        remaining = 0;
      } else {
        stars.add(_buildStar(Assets.icons.starHalf.path,));
      }
    }
    return Row(mainAxisSize: MainAxisSize.min, children: stars);
  }

  Widget _buildStar(String asset, {Color? color}) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 1),
      child: SvgPicture.asset(
        asset,
        width: size,
        height: size,
        colorFilter: color != null ? ColorFilter.mode(color, BlendMode.srcIn) : null,
      ),
    );
  }
}

class UserCommentCard extends StatelessWidget {
  final CommentModel comment;

  const UserCommentCard({super.key, required this.comment});

  @override
  Widget build(BuildContext context) {
    final String formattedDate = intl.DateFormat('yyyy/MM/dd', 'fa').format(comment.publishedAt);

    return Card(
      elevation: 1,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      // color: Colors.white,
      margin: const EdgeInsets.symmetric(vertical: 16.0),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text(
                  comment.userName,
                  style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                ),
                const Spacer(),
                Text(
                  formattedDate,
                  style: TextStyle(color: Colors.grey.shade600, fontSize: 12),
                ),
              ],
            ),
            const SizedBox(height: 4),
            CustomStarRating(rating: comment.rating, size: 18),
            const SizedBox(height: 10),
            Text(
              '"${comment.comment}"',
              style: const TextStyle(fontSize: 14, height: 1.6, color: Colors.black87),
            ),
            if (comment.uploadedImageUrls.isNotEmpty) ...[
              const SizedBox(height: 16),
              Text(
                'عکس‌های آپلود شده توسط کاربر',
                style: TextStyle(
                  fontWeight: FontWeight.normal,
                  color: AppColors.active,
                  fontSize: 14,
                ),
              ),
              const SizedBox(height: 20),
              SizedBox(
                height: 80,
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: comment.uploadedImageUrls.length,
                  itemBuilder: (context, index) {
                    final imageUrl = comment.uploadedImageUrls[index];
                    return Padding(
                      padding: const EdgeInsets.only(left: 8.0),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8.0),
                        child: Image.network(
                          imageUrl,
                          width: 80,
                          height: 80,
                          fit: BoxFit.cover,
                        ),
                      ),
                    );
                  },
                ),
              )
            ],
          ],
        ),
      ),
    );
  }
}