import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
import 'package:qr_flutter/qr_flutter.dart';

// Service to manage contact-related operations
class ContactService {
  Future<List<Contact>> fetchContacts() async {
    if (await FlutterContacts.requestPermission()) {
      return await FlutterContacts.getContacts(
          withProperties: true,
          withThumbnail: true,
          withAccounts: true,
          withGroups: true,
          withPhoto: true);
    }
    return [];
  }

  Future<List<Contact>> fetchFavoriteContacts() async {
    List<Contact> contacts = await fetchContacts();
    return contacts.where((contact) => contact.isStarred).toList();
  }

  Future<void> addNewContact(Contact contact) async {
    await FlutterContacts.insertContact(contact);
  }

  // Function to show an AlertDialog with a QR code for the contact's vCard
  void showContactQRCodeDialog(BuildContext context, Contact contact) {
    showDialog(
      barrierColor: Colors.white24,
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context) {
        return AlertDialog(
          backgroundColor: Colors.black,
          content: SizedBox(
            width: 200,
            height: 220,
            child: QrImageView(
              data: contact.toVCard(), // Generate vCard QR code
              version: QrVersions.auto,
              backgroundColor: Colors.white, // Make sure QR code is visible on black background
              size: 200.0,
            ),
          ),
        );
      },
    );
  }
}
