using System.ComponentModel.DataAnnotations;
using static System.Text.RegularExpressions.Regex;

namespace Core.Entities;

public class Student : IEquatable<Student>
{
	public int Id { get; set; }

    [Required]
    [StringLength(50,MinimumLength = 2)]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 2)]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Range(5,12)]
    [Display(Name = "Grade")]
    public int Grade { get; set;  }
    
    [EmailAddress]
    [Display(Name = "Email Address")]
    public string? Email { get; set; }

    [Phone]
    [Display(Name = "Phone Number")]
    public string? PhoneNumber { get; set; }

    [Display(Name = "TSA Year")]
    public int TsaYear { get; set; }

    [Display(Name = "State Id")]
    public string? StateId { get; set; }

    [Display(Name = "Regional Id")]
    public string? RegionalId { get; set;  }

    [Display(Name = "National Id")]
    public string? NationalId { get; set;  }

    [Display(Name = "Officer Role")]
    public OfficerRole? OfficerRole { get; set;  }

    public List<Team> Teams { get; set; } = null;
    public List<EventDefinition> RankedEvents { get; } = [];
    public List<StudentEventRanking> EventRankings { get; } = [];


    public string Name => FirstNameLastName;

    public string LastNameFirstName => $"{LastName}, {FirstName}";

    public string FirstNameLastName => $"{FirstName} {LastName}";

    public static Tuple<string, string> ParseNameParts(string fullName)
    {
        var match = Match(fullName, @"(.*),\s*(.*)");
        if (match.Success)
            return Tuple.Create(match.Groups[2].Value, match.Groups[1].Value);
        match = Match(fullName, @"(.*)\s*(.*)");
        return
            match.Success
                ? Tuple.Create(match.Groups[1].Value, match.Groups[2].Value)
                : new Tuple<string, string>(fullName, string.Empty);
    }

    public override string ToString()
    {
	    return FirstName;
    }

    public bool VotingDelegate => OfficerRole is Entities.OfficerRole.President or Entities.OfficerRole.VicePresident;

    public bool Equals(Student? other)
    {
        if (other is null) return false;
        if (ReferenceEquals(this, other)) return true;
        return Id == other.Id;
    }

    public override bool Equals(object? obj)
    {
        if (obj is null) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != GetType()) return false;
        return Equals((Student)obj);
    }

    public override int GetHashCode()
    {
        return Id;
    }
}