namespace RealCV.Application.Data;

/// <summary>
/// List of recognised UK higher education institutions.
/// Source: GOV.UK Register of Higher Education Providers
/// </summary>
public static class UKInstitutions
{
    /// <summary>
    /// Recognised UK universities and higher education providers.
    /// These are legitimate degree-awarding institutions.
    /// </summary>
    public static readonly HashSet<string> RecognisedInstitutions = new(StringComparer.OrdinalIgnoreCase)
    {
        // Russell Group Universities
        "University of Birmingham",
        "University of Bristol",
        "University of Cambridge",
        "Cardiff University",
        "Durham University",
        "University of Edinburgh",
        "University of Exeter",
        "University of Glasgow",
        "Imperial College London",
        "King's College London",
        "University of Leeds",
        "University of Liverpool",
        "London School of Economics",
        "London School of Economics and Political Science",
        "LSE",
        "University of Manchester",
        "Newcastle University",
        "University of Nottingham",
        "University of Oxford",
        "Queen Mary University of London",
        "Queen's University Belfast",
        "University of Sheffield",
        "University of Southampton",
        "University College London",
        "UCL",
        "University of Warwick",
        "University of York",

        // Other Major Universities
        "Aston University",
        "Leeds Beckett University",
        "Leeds Metropolitan University", // Former name of Leeds Beckett
        "University of Bath",
        "Birkbeck, University of London",
        "Bournemouth University",
        "University of Bradford",
        "University of Brighton",
        "Brunel University London",
        "University of Buckingham",
        "Canterbury Christ Church University",
        "City, University of London",
        "Coventry University",
        "Cranfield University",
        "De Montfort University",
        "University of Derby",
        "University of Dundee",
        "University of East Anglia",
        "UEA",
        "University of East London",
        "Edge Hill University",
        "University of Essex",
        "Falmouth University",
        "University of Greenwich",
        "Heriot-Watt University",
        "University of Hertfordshire",
        "University of Huddersfield",
        "University of Hull",
        "Keele University",
        "University of Kent",
        "Kingston University",
        "Lancaster University",
        "University of Leicester",
        "University of Lincoln",
        "Liverpool John Moores University",
        "Liverpool Hope University",
        "University of London",
        "London Metropolitan University",
        "London South Bank University",
        "Loughborough University",
        "Manchester Metropolitan University",
        "Middlesex University",
        "Northumbria University",
        "Norwich University of the Arts",
        "Nottingham Trent University",
        "Open University",
        "The Open University",
        "Oxford Brookes University",
        "University of Plymouth",
        "University of Portsmouth",
        "Queen Margaret University",
        "University of Reading",
        "Robert Gordon University",
        "Roehampton University",
        "Royal Holloway, University of London",
        "Royal Holloway",
        "University of Salford",
        "SOAS University of London",
        "SOAS",
        "Sheffield Hallam University",
        "University of South Wales",
        "University of St Andrews",
        "St Andrews",
        "Staffordshire University",
        "University of Stirling",
        "University of Strathclyde",
        "University of Sunderland",
        "University of Surrey",
        "University of Sussex",
        "Swansea University",
        "Teesside University",
        "Ulster University",
        "University of the West of England",
        "UWE Bristol",
        "University of the West of Scotland",
        "University of Westminster",
        "University of Winchester",
        "University of Wolverhampton",
        "University of Worcester",
        "Wrexham University",
        "York St John University",

        // Scottish Universities
        "University of Aberdeen",
        "Abertay University",
        "Edinburgh Napier University",
        "Glasgow Caledonian University",
        "University of the Highlands and Islands",

        // Welsh Universities
        "Aberystwyth University",
        "Bangor University",
        "University of South Wales",
        "Wrexham Glyndwr University",

        // Northern Ireland
        "Ulster University",
        "Queen's University Belfast",

        // Specialist Institutions
        "Royal Academy of Music",
        "Royal College of Art",
        "Royal College of Music",
        "Royal Northern College of Music",
        "Royal Veterinary College",
        "Goldsmiths, University of London",
        "Goldsmiths",
        "Courtauld Institute of Art",
        "London Business School",
        "LBS",
        "Guildhall School of Music and Drama",
        "Trinity Laban Conservatoire of Music and Dance",
        "Arts University Bournemouth",
        "University for the Creative Arts",
        "Ravensbourne University London",

        // Business Schools (accredited)
        "Henley Business School",
        "Warwick Business School",
        "Manchester Business School",
        "Said Business School",
        "Judge Business School",
        "Cass Business School",
        "Bayes Business School",
        "Imperial College Business School",
        "Cranfield School of Management",
        "Ashridge Business School",
        "Alliance Manchester Business School",
    };

    /// <summary>
    /// Common name variations and abbreviations mapped to official names.
    /// </summary>
    public static readonly Dictionary<string, string> NameVariations = new(StringComparer.OrdinalIgnoreCase)
    {
        ["Cambridge"] = "University of Cambridge",
        ["Oxford"] = "University of Oxford",
        ["Cambridge University"] = "University of Cambridge",
        ["Oxford University"] = "University of Oxford",
        ["Imperial"] = "Imperial College London",
        ["Imperial College"] = "Imperial College London",
        ["Kings College London"] = "King's College London",
        ["Kings London"] = "King's College London",
        ["KCL"] = "King's College London",
        ["Edinburgh"] = "University of Edinburgh",
        ["Manchester"] = "University of Manchester",
        ["Bristol"] = "University of Bristol",
        ["Warwick"] = "University of Warwick",
        ["Durham"] = "Durham University",
        ["Bath"] = "University of Bath",
        ["Exeter"] = "University of Exeter",
        ["York"] = "University of York",
        ["Leeds"] = "University of Leeds",
        ["Sheffield"] = "University of Sheffield",
        ["Birmingham"] = "University of Birmingham",
        ["Nottingham"] = "University of Nottingham",
        ["Southampton"] = "University of Southampton",
        ["Glasgow"] = "University of Glasgow",
        ["Liverpool"] = "University of Liverpool",
        ["Lancaster"] = "Lancaster University",
        ["Leicester"] = "University of Leicester",
        ["Surrey"] = "University of Surrey",
        ["Sussex"] = "University of Sussex",
        ["Reading"] = "University of Reading",
        ["Loughborough"] = "Loughborough University",
        ["Brunel"] = "Brunel University London",
        ["Kent"] = "University of Kent",
        ["Essex"] = "University of Essex",
        ["Strathclyde"] = "University of Strathclyde",
        ["Heriot Watt"] = "Heriot-Watt University",
        ["Heriot-Watt"] = "Heriot-Watt University",
        ["St Andrews University"] = "University of St Andrews",
        ["Saint Andrews"] = "University of St Andrews",
        ["Birkbeck"] = "Birkbeck, University of London",
        ["QMUL"] = "Queen Mary University of London",
        ["Queen Mary"] = "Queen Mary University of London",
        ["Royal Holloway University"] = "Royal Holloway, University of London",
        ["RHUL"] = "Royal Holloway, University of London",
        ["Leeds Beckett"] = "Leeds Beckett University",
        ["Leeds Met"] = "Leeds Beckett University",
        ["Leeds Metropolitan"] = "Leeds Beckett University",
    };

    /// <summary>
    /// Check if an institution is recognised. Handles common variations.
    /// </summary>
    public static bool IsRecognised(string institutionName)
    {
        if (string.IsNullOrWhiteSpace(institutionName))
            return false;

        var normalised = institutionName.Trim();

        // Direct match
        if (RecognisedInstitutions.Contains(normalised))
            return true;

        // Check variations
        if (NameVariations.TryGetValue(normalised, out var officialName))
            return RecognisedInstitutions.Contains(officialName);

        // Fuzzy match - check if any recognised institution contains the search term
        // or if the search term contains a recognised institution
        foreach (var institution in RecognisedInstitutions)
        {
            if (institution.Contains(normalised, StringComparison.OrdinalIgnoreCase) ||
                normalised.Contains(institution, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
        }

        return false;
    }

    /// <summary>
    /// Get the official name of an institution if found.
    /// </summary>
    public static string? GetOfficialName(string institutionName)
    {
        if (string.IsNullOrWhiteSpace(institutionName))
            return null;

        var normalised = institutionName.Trim();

        // Direct match
        if (RecognisedInstitutions.Contains(normalised))
            return normalised;

        // Check variations
        if (NameVariations.TryGetValue(normalised, out var officialName))
            return officialName;

        // Fuzzy match
        foreach (var institution in RecognisedInstitutions)
        {
            if (institution.Contains(normalised, StringComparison.OrdinalIgnoreCase) ||
                normalised.Contains(institution, StringComparison.OrdinalIgnoreCase))
            {
                return institution;
            }
        }

        return null;
    }
}
