import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_field_work/controller/finance/advance_payment_controller.dart';
import 'package:on_field_work/controller/project_controller.dart';
import 'package:on_field_work/helpers/utils/mixins/ui_mixin.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:on_field_work/helpers/widgets/custom_app_bar.dart';

class AdvancePaymentScreen extends StatefulWidget {
  const AdvancePaymentScreen({super.key});

  @override
  State<AdvancePaymentScreen> createState() => _AdvancePaymentScreenState();
}

class _AdvancePaymentScreenState extends State<AdvancePaymentScreen>
    with UIMixin {
  late final AdvancePaymentController controller;
  late final TextEditingController _searchCtrl;
  final FocusNode _searchFocus = FocusNode();
  final projectController = Get.find<ProjectController>();

  @override
  void initState() {
    super.initState();
    controller = Get.put(AdvancePaymentController());
    _searchCtrl = TextEditingController();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      final employeeId = Get.arguments?['employeeId'] ?? '';
      if (employeeId.isNotEmpty) {
        controller.fetchAdvancePayments(employeeId);
      }
    });

    controller.searchQuery.listen((q) {
      if (_searchCtrl.text != q) _searchCtrl.text = q;
    });
  }

  @override
  void dispose() {
    _searchCtrl.dispose();
    _searchFocus.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final Color appBarColor = contentTheme.primary;

    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: CustomAppBar(
        title: "Advance Payments",
        onBackPressed: () => Get.offNamed('/dashboard/finance'),
        backgroundColor: appBarColor,
      ),
      body: Stack(
        children: [
          // ===== TOP GRADIENT =====
          Container(
            height: 100,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  appBarColor,
                  appBarColor.withOpacity(0.0),
                ],
              ),
            ),
          ),

          // ===== MAIN CONTENT =====
          SafeArea(
            top: false,
            bottom: true,
            child: GestureDetector(
              onTap: () => FocusScope.of(context).unfocus(),
              child: RefreshIndicator(
                onRefresh: () async {
                  final emp = controller.selectedEmployee.value;
                  if (emp != null) {
                    await controller.fetchAdvancePayments(emp.id.toString());
                  }
                },
                color: Colors.white,
                backgroundColor: appBarColor,
                strokeWidth: 2.5,
                displacement: 60,
                child: SingleChildScrollView(
                  physics: const AlwaysScrollableScrollPhysics(),
                  child: Padding(
                    padding: EdgeInsets.only(
                      bottom: MediaQuery.of(context).padding.bottom + 20,
                    ),
                    child: Column(
                      children: [
                        // ===== SEARCH BAR FLOATING OVER GRADIENT =====
                        Padding(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 12, vertical: 8),
                          child: SizedBox(
                            height: 38,
                            child: TextField(
                              controller: _searchCtrl,
                              focusNode: _searchFocus,
                              onChanged: (v) =>
                                  controller.searchQuery.value = v.trim(),
                              decoration: InputDecoration(
                                contentPadding: const EdgeInsets.symmetric(
                                    horizontal: 12, vertical: 0),
                                prefixIcon: const Icon(Icons.search,
                                    size: 20, color: Colors.grey),
                                hintText: 'Search Employee...',
                                filled: true,
                                fillColor: Colors.white,
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(8),
                                  borderSide: BorderSide(
                                      color: Colors.grey.shade300, width: 1),
                                ),
                                enabledBorder: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(8),
                                  borderSide: BorderSide(
                                      color: Colors.grey.shade300, width: 1),
                                ),
                                focusedBorder: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(8),
                                  borderSide: BorderSide(
                                      color: appBarColor, width: 1.5),
                                ),
                              ),
                            ),
                          ),
                        ),

                        // ===== EMPLOYEE DROPDOWN =====
                        _buildEmployeeDropdown(context),

                        // ===== TOP BALANCE =====
                        _buildTopBalance(),

                        // ===== PAYMENTS LIST =====
                        _buildPaymentList(),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // ---------------- Employee Dropdown ----------------
  Widget _buildEmployeeDropdown(BuildContext context) {
    return Obx(() {
      if (controller.employees.isEmpty ||
          controller.selectedEmployee.value != null) {
        return const SizedBox.shrink();
      }

      return Container(
        margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: Colors.grey.shade300),
          borderRadius: BorderRadius.circular(10),
          boxShadow: [
            BoxShadow(
                color: Colors.black.withOpacity(0.05),
                blurRadius: 6,
                offset: const Offset(0, 3))
          ],
        ),
        constraints: BoxConstraints(
          maxHeight: MediaQuery.of(context).size.height * 0.4,
        ),
        child: ListView.separated(
          padding: const EdgeInsets.symmetric(vertical: 6),
          shrinkWrap: true,
          physics: const BouncingScrollPhysics(),
          itemCount: controller.employees.length,
          separatorBuilder: (_, __) =>
              Divider(height: 1, color: Colors.grey.shade200),
          itemBuilder: (_, i) => _buildEmployeeItem(controller.employees[i]),
        ),
      );
    });
  }

  Widget _buildEmployeeItem(dynamic e) {
    return InkWell(
      onTap: () {
        controller.selectEmployee(e);
        _searchCtrl.text = e.name;
        controller.searchQuery.value = e.name;
        FocusScope.of(context).unfocus();
        SystemChannels.textInput.invokeMethod('TextInput.hide');
        controller.employees.clear();
      },
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        child: Row(
          children: [
            CircleAvatar(
              radius: 18,
              backgroundColor: _avatarColorFor(e.name),
              child: Text(
                _initials(e.firstName, e.lastName),
                style: const TextStyle(color: Colors.white),
              ),
            ),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(e.name,
                      style: const TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.w600,
                          color: Colors.black87)),
                  if (e.email.isNotEmpty)
                    Text(e.email,
                        style: TextStyle(
                            fontSize: 13, color: Colors.grey.shade600)),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // ---------------- Current Balance ----------------
  Widget _buildTopBalance() {
    return Obx(() {
      if (controller.payments.isEmpty) return const SizedBox.shrink();
      final bal = controller.payments.first.balance.truncate();

      return Container(
        width: double.infinity,
        color: Colors.grey[100],
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            const Text(
              "Current Balance : ",
              style: TextStyle(
                fontWeight: FontWeight.w600,
                color: Colors.green,
                fontSize: 22,
              ),
            ),
            Text(
              "₹${_formatAmount(bal)}",
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.green,
                fontSize: 22,
              ),
            ),
          ],
        ),
      );
    });
  }

  // ---------------- Payments List ----------------
  Widget _buildPaymentList() {
    return Obx(() {
      if (controller.isLoading.value) {
        return const Padding(
          padding: EdgeInsets.only(top: 100),
          child: Center(child: CircularProgressIndicator(color: Colors.blue)),
        );
      }

      if (controller.selectedEmployee.value == null) {
        return const Padding(
          padding: EdgeInsets.only(top: 100),
          child: Center(child: Text("Please select an Employee")),
        );
      }

      if (controller.payments.isEmpty) {
        return const Padding(
          padding: EdgeInsets.only(top: 100),
          child: Center(
            child: Text("No advance payment transactions found."),
          ),
        );
      }

      return ListView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 6),
        itemCount: controller.payments.length,
        itemBuilder: (context, index) =>
            _buildPaymentItem(controller.payments[index]),
      );
    });
  }

  // ---------------- Payment Item ----------------
  Widget _buildPaymentItem(dynamic item) {
    final dateStr = (item.date ?? '').toString();
    DateTime? parsedDate;
    try {
      parsedDate = DateTime.parse(dateStr);
    } catch (_) {}

    final formattedDate = parsedDate != null
        ? DateFormat('dd MMM yyyy').format(parsedDate)
        : (dateStr.isNotEmpty ? dateStr : '—');

    final project = item.name ?? '';
    final desc = item.title ?? '';
    final amount = (item.amount ?? 0).toDouble();
    final isCredit = amount >= 0;
    final accentColor = isCredit ? Colors.green.shade700 : Colors.red.shade700;

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
      decoration: BoxDecoration(
        color: Colors.grey[100],
        border: Border(
          bottom: BorderSide(color: const Color(0xFFE0E0E0), width: 0.9),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Text(
                      formattedDate,
                      style:
                          TextStyle(fontSize: 12, color: Colors.grey.shade600),
                    ),
                  ],
                ),
                const SizedBox(height: 4),
                Text(
                  project.isNotEmpty ? project : 'No Project',
                  style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                    color: Colors.black,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  desc.isNotEmpty ? desc : 'No Details',
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: 13,
                    color: Colors.grey.shade700,
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(width: 8),
          Text(
            "${isCredit ? '+' : '-'} ₹${_formatAmount(amount)}",
            style: TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.bold,
              color: accentColor,
            ),
          ),
        ],
      ),
    );
  }

  // ---------------- Utilities ----------------
  String _initials(String? firstName, [String? lastName]) {
    if ((firstName?.isEmpty ?? true) && (lastName?.isEmpty ?? true)) return '?';
    return ((firstName?.isNotEmpty == true ? firstName![0] : '') +
            (lastName?.isNotEmpty == true ? lastName![0] : ''))
        .toUpperCase();
  }

  String _formatAmount(num amount) {
    final format = NumberFormat('#,##,###.##', 'en_IN');
    return format.format(amount);
  }

  static Color _avatarColorFor(String name) {
    final colors = [
      Colors.green,
      Colors.indigo,
      Colors.orange,
      Colors.blueGrey,
      Colors.deepPurple,
      Colors.teal,
      Colors.amber,
    ];
    final hash = name.codeUnits.fold(0, (p, e) => p + e);
    return colors[hash % colors.length];
  }
}
