#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <iomanip>

using namespace std;

struct Contact {
	string lastName;
	string firstName;
	string patronymic;
	string birthDate; 
	string address;
	string phone;
	string workplace;
	string position;
};


// Функция для автоматического формирования поздравления НЕ РАБОТАЕТ, ГАДОСТЬ
string generateBirthdayGreeting(const Contact& contact) {
	time_t timer;
	time(&timer);
	tm *year = localtime(&timer);
	string current_date = to_string(1900 + year->tm_year) + "-" + to_string(1 + year->tm_mon) + "-" + to_string(year->tm_mday);

	if (contact.birthDate.substr(5) == current_date.substr(5))
	{
		return "Уважаемый(ая) " + contact.firstName + " " + contact.patronymic + "! \nС Днем Рождения!\n";
	}
	else {
		return 0;
	}
}



// Вывод списка контактов (сортировка по фамилии)
void printSortedContacts(const vector<Contact>& contacts) {
	vector<pair<string, string>> sortedContacts;
	for (const auto& contact : contacts) {
		sortedContacts.push_back({ contact.lastName, contact.phone });
	}
	sort(sortedContacts.begin(), sortedContacts.end());

	cout << "Список контактов (фамилия - телефон):" << endl;
	for (const auto& item : sortedContacts) {
		cout << item.first << " - " << item.second << endl;
	}
}

// Поиск контактов по фамилии
vector<Contact> searchByLastName(const vector<Contact>& contacts, const string& lastName) {
	vector<Contact> result;
	for (const auto& contact : contacts) {
		if (contact.lastName == lastName) {
			result.push_back(contact);
		}
	}
	return result;
}


// Функция для поиска контактов по адресу
vector<Contact> searchByAddress(const vector<Contact>& contacts, const string& address) {
	vector<Contact> result;
	for (const auto& contact : contacts) {
		if (contact.address == address) {
			result.push_back(contact);
		}
	}
	return result;
}


int main() {
	vector<Contact> contacts;


	// Вывод поздравлений с днем рождения
	for (const auto& contact : contacts) {
		string greeting = generateBirthdayGreeting(contact);
		if (!greeting.empty()) {
			cout << greeting << endl;
		}
	}

	// Поиск по фамилии
	string searchLastName;
	cout << "Введите фамилию для поиска: ";
	cin >> searchLastName;
	vector<Contact> foundContacts = searchByLastName(contacts, searchLastName);
	for (const auto& contact : foundContacts) {
		cout << contact.lastName << " " << contact.firstName << " " << contact.patronymic << ", " << contact.birthDate << ", "
			<< contact.address << ", " << contact.phone << ", " << contact.workplace << ", " << contact.position << endl;
	}



	// Поиск по адресу
	string searchAddress;
	cout << "Введите адрес для поиска: ";
	cin.ignore(); 
	getline(cin, searchAddress); 
	vector<Contact> foundByAddress = searchByAddress(contacts, searchAddress);
	for (const auto& contact : foundByAddress)
	{
		cout << contact.lastName << " " << contact.firstName << " " << contact.patronymic << ", " << contact.birthDate << ", "
			<< contact.address << ", " << contact.phone << ", " << contact.workplace << ", " << contact.position << endl;

	}


	// Вывод отсортированного списка
	printSortedContacts(contacts);

	return 0;
}