import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../controller/admin/quality_controller.dart';

class DriverScorecardPage extends StatelessWidget {
  final String driverId;
  const DriverScorecardPage({super.key, required this.driverId});

  @override
  Widget build(BuildContext context) {
    QualityController controller = Get.put(QualityController());
    // Fetch data when page opens
    WidgetsBinding.instance.addPostFrameCallback((_) {
      controller.fetchDriverScorecard(driverId);
    });

    return Scaffold(
      appBar: AppBar(
        title: const Text('بطاقة أداء السائق',
            style: TextStyle(fontWeight: FontWeight.bold)),
        backgroundColor: Colors.blueAccent,
      ),
      body: GetBuilder<QualityController>(
        builder: (controller) {
          if (controller.isLoading) {
            return const Center(child: CircularProgressIndicator());
          }

          if (controller.scorecardData.isEmpty) {
            return const Center(
                child: Text('لا توجد بيانات لهذا السائق أو حدث خطأ.'));
          }

          var basicInfo = controller.scorecardData['basic_info'];
          var ridesStats = controller.scorecardData['rides_stats'];
          var rating = controller.scorecardData['rating'];
          var behavior = controller.scorecardData['behavior'];
          var complaints = controller.scorecardData['complaints'];
          num overallScore = controller.scorecardData['overall_score'] ?? 0;

          Color scoreColor = _getScoreColor(overallScore);

          return SingleChildScrollView(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // رأس البطاقة - معلومات السائق والتقييم الإجمالي
                Card(
                  elevation: 4,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15)),
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Column(
                      children: [
                        CircleAvatar(
                          radius: 40,
                          backgroundColor: Colors.grey.shade300,
                          child: const Icon(Icons.person,
                              size: 50, color: Colors.white),
                        ),
                        const SizedBox(height: 10),
                        Text(
                            '${basicInfo['first_name']} ${basicInfo['last_name']}',
                            style: const TextStyle(
                                fontSize: 22, fontWeight: FontWeight.bold)),
                        Text('هاتف: ${basicInfo['phone']}',
                            style: const TextStyle(color: Colors.grey)),
                        const Divider(height: 30),
                        Text('التقييم الشامل (Score)',
                            style: TextStyle(
                                fontSize: 18, color: Colors.grey.shade700)),
                        const SizedBox(height: 5),
                        Stack(
                          alignment: Alignment.center,
                          children: [
                            SizedBox(
                              width: 100,
                              height: 100,
                              child: CircularProgressIndicator(
                                value: overallScore / 100,
                                strokeWidth: 10,
                                backgroundColor: Colors.grey.shade200,
                                color: scoreColor,
                              ),
                            ),
                            Text('${overallScore.toStringAsFixed(1)}%',
                                style: TextStyle(
                                    fontSize: 24,
                                    fontWeight: FontWeight.bold,
                                    color: scoreColor)),
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
                const SizedBox(height: 15),

                // قسم إحصائيات الرحلات
                _buildSectionTitle('إحصائيات الرحلات (الإنجاز)'),
                Card(
                  elevation: 2,
                  child: ListTile(
                    leading: const Icon(Icons.drive_eta, color: Colors.blue),
                    title: const Text('نسبة الإنجاز'),
                    trailing: Text('${ridesStats['completion_rate']}%',
                        style: const TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold)),
                    subtitle: Text(
                        'إجمالي: ${ridesStats['total_rides']} | اكتمل: ${ridesStats['completed_rides']}\n'
                        'إلغاء سائق: ${ridesStats['driver_cancellations']} | إلغاء راكب: ${ridesStats['passenger_cancellations']}'),
                  ),
                ),

                const SizedBox(height: 10),

                // قسم التقييم والشكاوى
                _buildSectionTitle('رضا العملاء والشكاوى'),
                Row(
                  children: [
                    Expanded(
                      child: Card(
                        elevation: 2,
                        child: Padding(
                          padding: const EdgeInsets.all(12.0),
                          child: Column(
                            children: [
                              const Icon(Icons.star,
                                  color: Colors.orange, size: 30),
                              const SizedBox(height: 5),
                              Text('${rating.toString()}/5.0',
                                  style: const TextStyle(
                                      fontSize: 20,
                                      fontWeight: FontWeight.bold)),
                              const Text('متوسط التقييم',
                                  style: TextStyle(
                                      fontSize: 12, color: Colors.grey)),
                            ],
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      child: Card(
                        elevation: 2,
                        child: Padding(
                          padding: const EdgeInsets.all(12.0),
                          child: Column(
                            children: [
                              const Icon(Icons.warning,
                                  color: Colors.redAccent, size: 30),
                              const SizedBox(height: 5),
                              Text('${complaints['total_complaints']} شكوى',
                                  style: const TextStyle(
                                      fontSize: 20,
                                      fontWeight: FontWeight.bold)),
                              Text('${complaints['open_complaints']} مفتوحة',
                                  style: const TextStyle(
                                      fontSize: 12, color: Colors.red)),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),

                const SizedBox(height: 10),

                // قسم سلوك القيادة
                _buildSectionTitle('سلوك القيادة والتتبع'),
                Card(
                  elevation: 2,
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Column(
                      children: [
                        _buildBehaviorRow('متوسط سرعة السائق',
                            '${behavior['avg_max_speed']} كم/س', Icons.speed),
                        const Divider(),
                        _buildBehaviorRow(
                            'مرات الفرملة القاسية',
                            '${behavior['total_hard_brakes']}',
                            Icons.dangerous),
                        const Divider(),
                        _buildBehaviorRow(
                            'تسارع مفاجئ',
                            '${behavior['total_rapid_accel']}',
                            Icons.fast_forward),
                        const Divider(),
                        _buildBehaviorRow('تقييم السلوك الآلي',
                            '${behavior['avg_behavior_score']}%', Icons.memory),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _buildSectionTitle(String title) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
      child: Text(title,
          style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Colors.black87)),
    );
  }

  Widget _buildBehaviorRow(String title, String value, IconData icon) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Icon(icon, size: 20, color: Colors.grey.shade600),
            const SizedBox(width: 8),
            Text(title, style: const TextStyle(fontSize: 15)),
          ],
        ),
        Text(value,
            style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
      ],
    );
  }

  Color _getScoreColor(num score) {
    if (score >= 80) return Colors.green;
    if (score >= 60) return Colors.orange;
    return Colors.red;
  }
}
