import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_admin/constant/colors.dart';
import 'package:siro_admin/controller/admin/security_v2_controller.dart';
import 'package:intl/intl.dart';

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

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(SecurityV2Controller());

    return Scaffold(
      backgroundColor: AppColor.bg,
      appBar: AppBar(
        title: const Text('سجل العمليات (Audit Logs)',
            style: TextStyle(fontWeight: FontWeight.bold)),
        backgroundColor: AppColor.surface,
        elevation: 0,
        centerTitle: true,
      ),
      body: GetBuilder<SecurityV2Controller>(
        builder: (ctrl) {
          if (ctrl.isLoading) {
            return const Center(
                child: CircularProgressIndicator(color: AppColor.accent));
          }

          if (ctrl.auditLogs.isEmpty) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.security_rounded,
                      size: 64, color: AppColor.textSecondary.withOpacity(0.3)),
                  const SizedBox(height: 16),
                  const Text('لا توجد عمليات مسجلة حالياً',
                      style: TextStyle(color: AppColor.textSecondary)),
                  const SizedBox(height: 8),
                  const Text(
                      'تأكد من إنشاء جدول سجل العمليات في قاعدة البيانات',
                      style: TextStyle(
                          color: AppColor.textSecondary, fontSize: 10)),
                ],
              ),
            );
          }

          return ListView.builder(
            padding: const EdgeInsets.all(16),
            itemCount: ctrl.auditLogs.length,
            itemBuilder: (ctx, i) {
              final log = ctrl.auditLogs[i];
              return _buildLogItem(log);
            },
          );
        },
      ),
    );
  }

  Widget _buildLogItem(Map<String, dynamic> log) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColor.surface,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: AppColor.divider),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(log['admin_name'] ?? 'أدمن غير معروف',
                  style: const TextStyle(
                      color: AppColor.accent, fontWeight: FontWeight.bold)),
              Text(log['created_at'] ?? '',
                  style: const TextStyle(
                      color: AppColor.textSecondary, fontSize: 11)),
            ],
          ),
          const SizedBox(height: 8),
          Text(log['action'] ?? '',
              style: const TextStyle(
                  color: AppColor.textPrimary, fontWeight: FontWeight.w600)),
          if (log['details'] != null) ...[
            const SizedBox(height: 4),
            Text(log['details'],
                style: const TextStyle(
                    color: AppColor.textSecondary, fontSize: 12)),
          ],
          const SizedBox(height: 8),
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: AppColor.info.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(6),
                ),
                child: Text(log['table_name'] ?? '',
                    style: const TextStyle(
                        color: AppColor.info,
                        fontSize: 10,
                        fontWeight: FontWeight.bold)),
              ),
              const SizedBox(width: 8),
              Text('ID: ${log['record_id']}',
                  style: const TextStyle(
                      color: AppColor.textSecondary, fontSize: 10)),
            ],
          ),
        ],
      ),
    );
  }
}
