
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import '../theme/app_colors.dart';

class SakeRadarChart extends StatelessWidget {
  final Map<String, int> tasteStats;
  final Color primaryColor;

  const SakeRadarChart({
    super.key,
    required this.tasteStats,
    required this.primaryColor,
  });

  @override
  Widget build(BuildContext context) {
    final appColors = Theme.of(context).extension<AppColors>()!;

    // Default values if stats are missing
    final aroma = tasteStats['aroma']?.toDouble() ?? 3.0;
    final sweetness = tasteStats['sweetness']?.toDouble() ?? 3.0;
    final acidity = tasteStats['acidity']?.toDouble() ?? 3.0;
    final bitterness = tasteStats['bitterness']?.toDouble() ?? 3.0;
    final body = tasteStats['body']?.toDouble() ?? 3.0;

    return AspectRatio(
      aspectRatio: 1.3,
      child: RadarChart(
        RadarChartData(
          radarTouchData: RadarTouchData(enabled: false),
          dataSets: [
            RadarDataSet(
              fillColor: primaryColor.withValues(alpha: 0.2),
              borderColor: Theme.of(context).brightness == Brightness.dark
                  ? Colors.white.withValues(alpha: 0.8) // High contrast white
                  : primaryColor.withValues(alpha: 0.7),
              entryRadius: 3,
              dataEntries: [
                RadarEntry(value: aroma),
                RadarEntry(value: sweetness),
                RadarEntry(value: acidity),
                RadarEntry(value: bitterness),
                RadarEntry(value: body),
              ],
              borderWidth: 3,
            ),
          ],
          radarBackgroundColor: Colors.transparent,
          borderData: FlBorderData(show: false),
          radarBorderData: const BorderSide(color: Colors.transparent),
          titlePositionPercentageOffset: 0.2,
          titleTextStyle: Theme.of(context).textTheme.labelSmall?.copyWith(
            color: appColors.brandPrimary, // CRITICAL FIX: Use brandPrimary for all themes
            fontSize: 10,
            fontWeight: FontWeight.bold
          ),
          getTitle: (index, angle) {
            String label;
            switch (index) {
              case 0:
                label = '香り';
                break;
              case 1:
                label = '甘み';
                break;
              case 2:
                label = '酸味';
                break;
              case 3:
                label = 'キレ';
                break;
              case 4:
                label = 'コク';
                break;
              default:
                return const RadarChartTitle(text: '');
            }

            return RadarChartTitle(
              text: label,
              angle: angle,
            );
          },
          tickCount: 4,
          ticksTextStyle: const TextStyle(color: Colors.transparent, fontSize: 0),
          tickBorderData: const BorderSide(color: Colors.transparent),
          gridBorderData: BorderSide(
            color: Theme.of(context).brightness == Brightness.dark
                ? Colors.white.withValues(alpha: 0.3) // Brighter grid
                : primaryColor.withValues(alpha: 0.2),
            width: 1
          ),
        ),
      ),
    );
  }
}
