/// Widget: Star Rating Selector
///
/// Interactive 5-star rating widget with hover effects and visual feedback.
library;

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:worker/core/constants/ui_constants.dart';

/// Star Rating Selector Widget
///
/// Displays 5 clickable stars with:
/// - Empty stars by default (FontAwesomeIcons.star)
/// - Filled stars when selected (FontAwesomeIcons.solidStar)
/// - Hover effects (scale 1.1)
/// - Rating label below showing text
/// - Callback when rating changes
class StarRatingSelector extends StatefulWidget {
  /// Current selected rating (0-5)
  final int rating;

  /// Callback when rating changes
  final ValueChanged<int> onRatingChanged;

  const StarRatingSelector({
    super.key,
    required this.rating,
    required this.onRatingChanged,
  });

  @override
  State<StarRatingSelector> createState() => _StarRatingSelectorState();
}

class _StarRatingSelectorState extends State<StarRatingSelector> {
  // Rating label text mapping
  static const Map<int, String> _ratingLabels = {
    0: 'Chưa chọn đánh giá',
    1: 'Rất không hài lòng',
    2: 'Không hài lòng',
    3: 'Bình thường',
    4: 'Hài lòng',
    5: 'Rất hài lòng',
  };

  // Star colors (semantic - keep as AppColors)
  static const Color _starUnselected = Color(0xFFe0e0e0);
  static const Color _starHover = Color(0xFFffc107);
  static const Color _starSelected = Color(0xFFff9800);
  static const Color _labelBackgroundSelected = Color(0xFFfff3e0);

  int? _hoverRating;

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    return Column(
      children: [
        // Stars Row
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: List.generate(5, (index) {
            final starIndex = index + 1;
            final isSelected = starIndex <= widget.rating;
            final isHovered = _hoverRating != null && starIndex <= _hoverRating!;

            return MouseRegion(
              onEnter: (_) => setState(() => _hoverRating = starIndex),
              onExit: (_) => setState(() => _hoverRating = null),
              cursor: SystemMouseCursors.click,
              child: GestureDetector(
                onTap: () => widget.onRatingChanged(starIndex),
                child: AnimatedScale(
                  scale: isHovered ? 1.1 : 1.0,
                  duration: AppDuration.short,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 6),
                    child: Icon(
                      isSelected
                          ? FontAwesomeIcons.solidStar
                          : FontAwesomeIcons.star,
                      size: 36,
                      color: isHovered
                          ? _starHover
                          : (isSelected ? _starSelected : _starUnselected),
                    ),
                  ),
                ),
              ),
            );
          }),
        ),

        const SizedBox(height: 12),

        // Rating Label
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
          decoration: BoxDecoration(
            color: widget.rating > 0
                ? _labelBackgroundSelected
                : colorScheme.surfaceContainerHighest,
            borderRadius: BorderRadius.circular(20),
          ),
          child: Text(
            _ratingLabels[widget.rating] ?? '',
            style: TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w600,
              color: widget.rating > 0 ? _starSelected : colorScheme.onSurfaceVariant,
            ),
          ),
        ),
      ],
    );
  }
}
