import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../constant/colors.dart';
import '../../../controller/home/captin/behavior_controller.dart';

class BehaviorPage extends StatelessWidget {
  const BehaviorPage({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(DriverBehaviorController());
    controller.fetchDriverBehavior();
    final theme = Theme.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Driver Behavior'.tr),
        centerTitle: true,
      ),
      body: Obx(() {
        if (controller.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }

        return SingleChildScrollView(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Card(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12)),
                elevation: 4,
                child: Padding(
                  padding: const EdgeInsets.all(16),
                  child: Column(
                    children: [
                      Text("Overall Behavior Score".tr,
                          style: theme.textTheme.titleLarge
                              ?.copyWith(fontWeight: FontWeight.bold)),
                      const SizedBox(height: 10),
                      Text(
                          "${controller.overallScore.value.toStringAsFixed(1)} / 100",
                          style: TextStyle(
                              fontSize: 28,
                              fontWeight: FontWeight.bold,
                              color: AppColor.primaryColor)),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 20),
              Text("Last 10 Trips".tr,
                  style: theme.textTheme.titleMedium
                      ?.copyWith(fontWeight: FontWeight.bold)),
              const SizedBox(height: 10),
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: controller.lastTrips.length,
                itemBuilder: (context, index) {
                  var trip = controller.lastTrips[index];
                  return Card(
                    elevation: 3,
                    child: ListTile(
                      leading: CircleAvatar(
                        backgroundColor: AppColor.primaryColor,
                        child: Text("${index + 1}",
                            style: const TextStyle(color: Colors.white)),
                      ),
                      title: Text("Trip ID: ${trip['trip_id']}"),
                      subtitle: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                              "${'Behavior Score'.tr}: ${trip['behavior_score']}"),
                          Text("${'Max Speed'.tr}: ${trip['max_speed']} km/h"),
                          Text("${'Hard Brake'.tr}s: ${trip['hard_brakes']}"),
                          Text(
                              "${'Distance'.tr}: ${trip['total_distance']} km"),
                        ],
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        );
      }),
    );
  }
}
