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

/// Shared helpers for vehicle / policy search API dropdown results.
class PolicySearchUtils {
  PolicySearchUtils._();

  static List<Map<String, dynamic>> normalizeData(dynamic data) {
    if (data == null) return [];
    if (data is List) {
      final out = <Map<String, dynamic>>[];
      for (final item in data) {
        if (item is Map) {
          out.add(Map<String, dynamic>.from(item));
        }
      }
      return out;
    }
    if (data is Map) {
      return [Map<String, dynamic>.from(data)];
    }
    return [];
  }

  static String? rcNoFromRow(Map<String, dynamic> row) {
    final rc = (row['rc_no'] ??
            row['reg_no'] ??
            row['vehicle_number'] ??
            row['registration_no'] ??
            '')
        .toString()
        .trim();
    return rc.isEmpty ? null : rc;
  }

  static String insuredNameFromRow(Map<String, dynamic> row) {
    return (row['customer_name'] ??
            row['insured_name'] ??
            row['proposer_name'] ??
            row['name'] ??
            '')
        .toString()
        .trim();
  }

  static String policyNumberFromRow(Map<String, dynamic> row) {
    return (row['policy_number'] ?? '').toString().trim();
  }

  /// Value for the "Search By Vehicle Number" input after selection.
  static String vehicleInputValueOnSelect(Map<String, dynamic> row) {
    final rc = rcNoFromRow(row);
    if (rc != null && rc.isNotEmpty) return rc;
    final policy = policyNumberFromRow(row);
    return policy.isNotEmpty ? policy : '';
  }
}

/// Dropdown list for vehicle or policy search — matches [AddClaimStepperDialog].
class PolicySearchResultsList extends StatelessWidget {
  static const Color _border = Color(0xFFE2E8F0);
  static const Color _muted = Color(0xFF64748B);
  static const Color _titleColor = Color(0xFF0F172A);

  const PolicySearchResultsList({
    super.key,
    required this.rows,
    required this.fromVehicle,
    required this.onSelected,
    this.height = 150,
    this.borderRadius = 10,
  });

  final List<Map<String, dynamic>> rows;
  final bool fromVehicle;
  final ValueChanged<Map<String, dynamic>> onSelected;
  final double height;
  final double borderRadius;

  @override
  Widget build(BuildContext context) {
    if (rows.isEmpty) return const SizedBox.shrink();

    return Container(
      margin: const EdgeInsets.only(top: 6),
      height: height,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: _border),
        borderRadius: BorderRadius.circular(borderRadius),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.05),
            blurRadius: 6,
          ),
        ],
      ),
      child: ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: rows.length,
        itemBuilder: (context, index) {
          final row = rows[index];
          final policy = PolicySearchUtils.policyNumberFromRow(row);
          final rcNo = PolicySearchUtils.rcNoFromRow(row) ?? '';
          final name = PolicySearchUtils.insuredNameFromRow(row);
          final title = policy.isNotEmpty ? policy : rcNo;

          return InkWell(
            onTap: () => onSelected(row),
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  if (fromVehicle) ...[
                    Text(
                      rcNo.isNotEmpty ? rcNo : '-',
                      style: GoogleFonts.inter(
                        fontSize: 12,
                        fontWeight: FontWeight.w600,
                        color: _titleColor,
                      ),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    if (policy.isNotEmpty)
                      Padding(
                        padding: const EdgeInsets.only(top: 2),
                        child: Text(
                          policy,
                          style: GoogleFonts.inter(
                            fontSize: 10,
                            color: _muted,
                          ),
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                  ] else ...[
                    Text(
                      title.isNotEmpty ? title : '-',
                      style: GoogleFonts.inter(
                        fontSize: 12,
                        fontWeight: FontWeight.w600,
                        color: _titleColor,
                      ),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    if (name.isNotEmpty)
                      Padding(
                        padding: const EdgeInsets.only(top: 2),
                        child: Text(
                          name,
                          style: GoogleFonts.inter(
                            fontSize: 10,
                            color: _muted,
                          ),
                        ),
                      ),
                  ],
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
