using System.ComponentModel.DataAnnotations;

namespace SilverLabs.Website.Models;

public class DeveloperApplication : IValidatableObject
{
    [Required(ErrorMessage = "Full name is required")]
    [StringLength(100, MinimumLength = 2)]
    public string FullName { get; set; } = string.Empty;

    [EmailAddress(ErrorMessage = "Invalid email address")]
    public string? Email { get; set; }

    [Required(ErrorMessage = "Username is required")]
    [RegularExpression(@"^[a-zA-Z0-9_-]{3,30}$", ErrorMessage = "Username must be 3-30 characters, letters, numbers, hyphens and underscores only")]
    public string DesiredUsername { get; set; } = string.Empty;

    [Required(ErrorMessage = "Timezone is required")]
    public string Timezone { get; set; } = string.Empty;

    [Required(ErrorMessage = "Please select a role")]
    public ApplicationRole Role { get; set; }

    [Required(ErrorMessage = "Please select at least one platform")]
    [MinLength(1, ErrorMessage = "Please select at least one platform")]
    public List<string> Platforms { get; set; } = new();

    [Required(ErrorMessage = "Password is required")]
    [StringLength(100, MinimumLength = 8, ErrorMessage = "Password must be at least 8 characters")]
    public string Password { get; set; } = string.Empty;

    [Required(ErrorMessage = "Please confirm your password")]
    [Compare("Password", ErrorMessage = "Passwords do not match")]
    public string ConfirmPassword { get; set; } = string.Empty;

    // Tester-specific
    public int? InternetUnderstanding { get; set; }
    public int? EnjoysTesting { get; set; }

    // Developer-specific
    public string? ExperienceRange { get; set; }
    public List<string> SelectedSkills { get; set; } = new();

    // Shared optional
    public string? AdditionalNotes { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Role == ApplicationRole.Tester)
        {
            if (!InternetUnderstanding.HasValue || InternetUnderstanding < 1 || InternetUnderstanding > 5)
                yield return new ValidationResult("Please rate your internet understanding", new[] { nameof(InternetUnderstanding) });

            if (!EnjoysTesting.HasValue || EnjoysTesting < 1 || EnjoysTesting > 5)
                yield return new ValidationResult("Please rate your enthusiasm for testing", new[] { nameof(EnjoysTesting) });
        }
        else if (Role == ApplicationRole.Developer)
        {
            if (string.IsNullOrWhiteSpace(ExperienceRange))
                yield return new ValidationResult("Please select your experience level", new[] { nameof(ExperienceRange) });

            if (SelectedSkills.Count == 0)
                yield return new ValidationResult("Please select at least one skill", new[] { nameof(SelectedSkills) });
        }
    }
}

public enum ApplicationRole
{
    Tester,
    Developer
}
