import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:lba/gen/assets.gen.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 = 20.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.starHalf.path));
        remaining = 0;
      } else {
        stars.add(_buildStar(Assets.icons.star.path));
      }
    }

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: stars,
    );
  }

  Widget _buildStar(String asset) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 2),
      child: SvgPicture.asset(
        asset,
        width: size,
        height: size,
      ),
    );
  }
}
