require 'date'
require_relative 'person'
require_relative 'student'
require_relative 'student_short'
require_relative 'student_repository'
require_relative 'binary_search_tree'

def test_classes
  person = Person.new(id: '1', git: 'https://github.com/example', phone: '+798912465', telegram: '@example_user', email: 'test@example.com')
  puts "Person Contact Info: #{person.get_first_contact}"

  student = Student.new(
    id: '2',
    git: 'https://github.com/student_example',
    surname: 'Иванов',
    name: 'Иван',
    patronymic: 'Иванович',
    birth_date: Date.new(2000, 5, 15),
    phone: '+79891234567',
    telegram: '@student_ivan',
    email: 'ivanov@example.com'
  )
  puts "Student Full Info: #{student.to_s}"
  puts "Student Initials: #{student.surname_initials}"
  puts "Student Contact Info: #{student.get_first_contact}"

  student_short = StudentShort.from_student(student)
  puts "Student Short Info: #{student_short.to_s}"

  file_path = 'students.txt'
  students = StudentRepository.read_from_txt(file_path)
  puts "Read Students from File:"
  students.each { |s| puts s.to_s }

  output_path = 'output_students.txt'
  StudentRepository.write_to_txt(output_path, students)
  puts "Students written to file: #{output_path}"
end

def test_parsing
  student_string = 'Иванов Иван Иванович | ID: 3 | Phone: +79876543210 | Telegram: @ivan_user | Email: ivan@example.com | Git: https://github.com/ivanov | Birth Date: 2001-01-01'
  student = Student.from_string(student_string)
  puts "Parsed Student: #{student.to_s}"

  short_string = 'Иванов И.И., Contact: Phone: +79876543210'
  student_short = StudentShort.from_string('4', short_string)
  puts "Parsed StudentShort: #{student_short.to_s}"
end

def test_binary_search_tree
  bst = BinarySearchTree.new

  student1 = Student.new(
    id: '1',
    git: 'https://github.com/student1',
    surname: 'Смирнов',
    name: 'Алексей',
    patronymic: 'Иванович',
    birth_date: Date.new(1999, 2, 15),
    phone: '+123456789',
    telegram: '@student1',
    email: 'student1@example.com'
  )
  student2 = Student.new(
    id: '2',
    git: 'https://github.com/student2',
    surname: 'Иванов',
    name: 'Иван',
    patronymic: 'Сергеевич',
    birth_date: Date.new(2001, 5, 10),
    phone: '+987654321',
    telegram: '@student2',
    email: 'student2@example.com'
  )

  bst.add(student1)
  bst.add(student2)

  puts "Students in BST (sorted by birth_date):"
  bst.each { |student| puts student.to_s }
end

if __FILE__ == $0
  puts "=== Testing Classes ==="
  test_classes

  puts "\n=== Testing Parsing ==="
  test_parsing

  puts "\n=== Testing Binary Search Tree ==="
  test_binary_search_tree
end