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

import '../../../models/taxi_model.dart';

/// Individual taxi card widget displaying taxi information with call and favorite functionality
class TaxiCardWidget extends StatelessWidget {
  final TaxiModel taxi;
  final bool isFavorite;
  final Function(String) onFavoriteToggle;

  const TaxiCardWidget({
    super.key,
    required this.taxi,
    required this.isFavorite,
    required this.onFavoriteToggle,
  });

  /// Launch phone call
  Future<void> _makePhoneCall(String phoneNumber) async {
    final Uri launchUri = Uri(scheme: 'tel', path: phoneNumber);
    try {
      if (await canLaunchUrl(launchUri)) {
        await launchUrl(launchUri);
      }
    } catch (e) {
      // Handle error silently or show user feedback
    }
  }

  /// Capitalize first letter of shift for display
  String _formatShift(String shift) {
    if (shift.isEmpty) return '';
    return shift[0].toUpperCase() + shift.substring(1);
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      elevation: 2,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Header Row: Name and Favorite Button
            Row(
              children: [
                Expanded(
                  child: Text(
                    taxi.name,
                    style: theme.textTheme.titleMedium?.copyWith(
                      fontWeight: FontWeight.bold,
                      color: const Color(0xFF101820),
                    ),
                  ),
                ),
                IconButton(
                  onPressed: () => onFavoriteToggle(taxi.id),
                  icon: Icon(
                    isFavorite ? Icons.favorite : Icons.favorite_border,
                    color:
                        isFavorite
                            ? const Color(0xFFFEE715)
                            : theme.iconTheme.color,
                  ),
                  tooltip:
                      isFavorite ? 'Remove from favorites' : 'Add to favorites',
                ),
              ],
            ),
            const SizedBox(height: 8),

            // Phone Row with Call Button
            Row(
              children: [
                Icon(Icons.phone, size: 20, color: theme.iconTheme.color),
                const SizedBox(width: 8),
                Expanded(
                  child: Text(
                    taxi.phone,
                    style: theme.textTheme.bodyMedium?.copyWith(
                      color: const Color(0xFF101820),
                    ),
                  ),
                ),
                ElevatedButton.icon(
                  onPressed: () => _makePhoneCall(taxi.phone),
                  icon: const Icon(Icons.call, size: 18),
                  label: const Text('Call'),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFFFEE715),
                    foregroundColor: const Color(0xFF101820),
                    padding: const EdgeInsets.symmetric(
                      horizontal: 16,
                      vertical: 8,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 8),

            // Location and Shift Info
            Row(
              children: [
                // Corregimiento
                Expanded(
                  child: Row(
                    children: [
                      Icon(
                        Icons.location_on,
                        size: 16,
                        color: theme.iconTheme.color?.withAlpha(153),
                      ),
                      const SizedBox(width: 4),
                      Text(
                        taxi.corregimiento,
                        style: theme.textTheme.bodySmall?.copyWith(
                          color: theme.textTheme.bodySmall?.color?.withAlpha(
                            153,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                // Shift
                Row(
                  children: [
                    Icon(
                      Icons.schedule,
                      size: 16,
                      color: theme.iconTheme.color?.withAlpha(153),
                    ),
                    const SizedBox(width: 4),
                    Text(
                      _formatShift(taxi.shift),
                      style: theme.textTheme.bodySmall?.copyWith(
                        color: theme.textTheme.bodySmall?.color?.withAlpha(153),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
