import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../../controller/admin/complaint_controller.dart';
import '../../widgets/my_scafold.dart';
import '../../widgets/elevated_btn.dart';
import '../../widgets/my_textField.dart';

class ComplaintListPage extends StatelessWidget {
  ComplaintListPage({super.key});

  final ComplaintController controller = Get.put(ComplaintController());

  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      title: 'إدارة الشكاوى'.tr,
      isleading: true,
      body: [
        Obx(() {
          if (controller.delayedComplaints.isNotEmpty) {
            return Container(
              margin: const EdgeInsets.all(16),
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: AppColor.danger.withOpacity(0.1),
                borderRadius: BorderRadius.circular(16),
                border: Border.all(color: AppColor.danger.withOpacity(0.3)),
              ),
              child: Column(
                children: [
                  Row(
                    children: [
                      const Icon(Icons.warning_amber_rounded, color: AppColor.danger),
                      const SizedBox(width: 12),
                      Expanded(
                        child: Text(
                          'هناك ${controller.delayedComplaints.length} شكاوى لم يتم حلها منذ أكثر من أسبوع!',
                          style: AppStyle.body.copyWith(color: AppColor.danger, fontWeight: FontWeight.bold),
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 12),
                  Obx(() => MyElevatedButton(
                    title: controller.showOnlyDelayed.value ? 'عرض جميع الشكاوى' : 'عرض الشكاوى المتأخرة فقط',
                    onPressed: () => controller.showOnlyDelayed.toggle(),
                    kolor: AppColor.danger,
                  )),
                ],
              ),
            );
          }
          return const SizedBox.shrink();
        }),
        Obx(() {
          final list = controller.showOnlyDelayed.value ? controller.delayedComplaints : controller.complaintList;
          if (controller.isLoading.value && list.isEmpty) {
            return const Center(child: CircularProgressIndicator());
          }
          return RefreshIndicator(
            onRefresh: () => controller.getComplaints(),
            child: ListView.builder(
              padding: const EdgeInsets.fromLTRB(16, 16, 16, 80),
              itemCount: list.length,
              itemBuilder: (context, index) {
                final complaint = list[index];
                return _buildComplaintCard(context, complaint);
              },
            ),
          );
        }),
      ],
    );
  }

  Widget _buildComplaintCard(BuildContext context, dynamic c) {
    Color statusColor = _getStatusColor(c['statusComplaint']);
    
    return Container(
      margin: const EdgeInsets.only(bottom: 16),
      decoration: AppStyle.cardDecoration,
      child: ExpansionTile(
        tilePadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        leading: _buildStatusIndicator(c['statusComplaint'], statusColor),
        title: Text(
          c['description']?.toString() ?? 'بدون وصف',
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: AppStyle.title,
        ),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 4),
            Text(
              'النوع: ${c['complaint_type'] ?? 'عام'} | الرحلة: ${c['ride_id']}',
              style: AppStyle.caption,
            ),
            const SizedBox(height: 4),
            Row(
              children: [
                Icon(Icons.person_rounded, size: 12, color: AppColor.textSecondary),
                const SizedBox(width: 4),
                Text(c['passengerName'] ?? 'غير معروف', style: AppStyle.caption),
                const SizedBox(width: 12),
                Icon(Icons.drive_eta_rounded, size: 12, color: AppColor.textSecondary),
                const SizedBox(width: 4),
                Text(c['driverName'] ?? 'غير معروف', style: AppStyle.caption),
              ],
            ),
          ],
        ),
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Divider(color: AppColor.divider),
                _buildInfoRow('الوصف', c['description'] ?? 'لا يوجد وصف'),
                const SizedBox(height: 12),
                _buildInfoRow('الحل الحالي', c['resolution'] ?? 'لم يتم الحل بعد'),
                const SizedBox(height: 12),
                _buildRideDetails(c),
                const SizedBox(height: 24),
                Row(
                  children: [
                    Expanded(
                      child: MyElevatedButton(
                        title: 'تحديث الحالة / حل الشكوى',
                        onPressed: () => _showResolveDialog(context, c),
                        kolor: AppColor.accent,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildStatusIndicator(String? status, Color color) {
    return Container(
      width: 40,
      height: 40,
      decoration: BoxDecoration(
        color: color.withOpacity(0.12),
        shape: BoxShape.circle,
        border: Border.all(color: color.withOpacity(0.25)),
      ),
      child: Icon(
        status == 'Resolved' ? Icons.check_circle_rounded : Icons.pending_rounded,
        color: color,
        size: 20,
      ),
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(label, style: AppStyle.caption.copyWith(color: AppColor.accent)),
        const SizedBox(height: 4),
        Text(value, style: AppStyle.body),
      ],
    );
  }

  Widget _buildRideDetails(dynamic c) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: AppColor.surfaceElevated,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColor.divider),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildSmallStat('السعر', '${c['priceOfRide']} ل.س'),
              _buildSmallStat('التقييم', '${c['avgRatingDriverFromPassengers'] ?? 0}★'),
              _buildSmallStat('النوع', c['ascarType'] ?? 'N/A'),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildSmallStat(String label, String value) {
    return Column(
      children: [
        Text(label, style: AppStyle.caption.copyWith(fontSize: 10)),
        const SizedBox(height: 2),
        Text(value, style: AppStyle.number.copyWith(fontSize: 12)),
      ],
    );
  }

  Color _getStatusColor(String? status) {
    switch (status) {
      case 'Open': return AppColor.danger;
      case 'In Progress': return AppColor.warning;
      case 'Resolved': return AppColor.success;
      default: return AppColor.textSecondary;
    }
  }

  void _showResolveDialog(BuildContext context, dynamic c) {
    final TextEditingController resController = TextEditingController(text: c['resolution']);
    String selectedStatus = c['statusComplaint'] ?? 'Open';

    Get.bottomSheet(
      StatefulBuilder(
        builder: (context, setModalState) => Container(
          padding: const EdgeInsets.all(24),
          decoration: const BoxDecoration(
            color: AppColor.surfaceElevated,
            borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('تحديث حالة الشكوى', style: AppStyle.headTitle),
              const SizedBox(height: 20),
              _buildStatusDropdown(selectedStatus, (val) {
                setModalState(() => selectedStatus = val!);
              }),
              const SizedBox(height: 20),
              MyTextForm(
                controller: resController,
                label: 'قرار الحل / الملاحظات',
                hint: 'اكتب تفاصيل الحل هنا...',
                type: TextInputType.multiline,
                prefixIcon: Icons.gavel_rounded,
              ),
              const SizedBox(height: 24),
              MyElevatedButton(
                title: 'حفظ التحديث',
                onPressed: () async {
                  bool success = await controller.updateComplaintStatus(
                    c['id'].toString(), 
                    selectedStatus, 
                    resController.text
                  );
                  if (success) Get.back();
                },
              ),
              const SizedBox(height: 20),
            ],
          ),
        ),
      ),
      isScrollControlled: true,
    );
  }

  Widget _buildStatusDropdown(String current, Function(String?) onChanged) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: AppColor.surface,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColor.divider),
      ),
      child: DropdownButtonHideUnderline(
        child: DropdownButton<String>(
          value: current,
          isExpanded: true,
          dropdownColor: AppColor.surfaceElevated,
          items: ['Open', 'In Progress', 'Resolved']
              .map((s) => DropdownMenuItem(value: s, child: Text(s.tr, style: AppStyle.body)))
              .toList(),
          onChanged: onChanged,
        ),
      ),
    );
  }
}
