import 'package:flutter/material.dart';
import 'package:business_panel/core/config/app_colors.dart';
import 'package:business_panel/gen/assets.gen.dart';
import 'package:business_panel/domain/entities/comment_entity.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));
        remaining -= 1;
      } else if (remaining >= 0.5) {
        stars.add(_buildStar(Assets.icons.starHalf));
        remaining = 0;
      } else {
        stars.add(_buildStar(Assets.icons.star));
      }
    }
    return Row(mainAxisSize: MainAxisSize.min, children: stars);
  }

  Widget _buildStar(String asset) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 1),
      child: SvgPicture.asset(
        asset,
        width: size,
        height: size,
        colorFilter: const ColorFilter.mode(Colors.amber, BlendMode.srcIn),
      ),
    );
  }
}

class UserCommentCard extends StatelessWidget {
  final CommentEntity comment;

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

  @override
  Widget build(BuildContext context) {
    final String formattedDate = comment.persianDate;

    return Card(
      elevation: 1,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      margin: const EdgeInsets.symmetric(vertical: 8.0),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Text(
                  comment.userFullName,
                  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.score, 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: 12),
              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,
                          errorBuilder: (context, error, stackTrace) => Container(
                              color: Colors.grey.shade200,
                              child: const Icon(Icons.error)),
                        ),
                      ),
                    );
                  },
                ),
              )
            ],
          ],
        ),
      ),
    );
  }
}