const global options = ["Geology", "Geophysics", "Geobiology", "Geochemistry", "Planetary Science", "ESE"] 

# find matching names, return matching score and index
function matchNames(student_name,fac_names)
    mc  = 0
    bestMatch = "" # best match!
    subname = split(strip(student_name)," ")
    for s in fac_names
        cc = 0
        fac_sub = split(strip(s)," ")
        for sub in lowercase.(subname), subf in lowercase.(fac_sub)
            # Could use regex or something, just being lazy...
            cc += occursin(sub, subf)
            cc += occursin(subf, sub)
        end
        if cc > mc
            mc = cc
            bestMatch = s
        end
    end
    return mc, bestMatch
end

"Load student table generated by Julie"
function loadStudents(xlsFile)
    students = Dict()
    for option in options
        students[option] = DataFrame(XLSX.readtable(xlsFile, option)...)
    end
    return students
end

"Check student submissions"
function checkStudents(students, submissions, thresh=3)
    for option in options
        for i = 1:size(students[option],1)
            foundEmail = students[option]."Email"[i] in submissions."Email Address"
            if !foundEmail
                # Try matching names:
                sName = students[option]."Student Name"[i]
                mc, bestMatch = matchNames(sName,submissions."Your Name")
                if mc<thresh
                    println(option, ": ", sName,  " ", students[option]."Year Of Study"[i], " , matchScore: ", mc, ", Best match: ", bestMatch )
                end
            end
        end
    end
end 

"find matches for students (for a class and whether faculty and students matched in preference)"
function find_matched_students(df_classes, df_students, row, course)
    ind_matched = findall(df_classes."Class Name".==row[FacTA_class[course]])[1]
    #println(ind_matched)
    df_classes[ind_matched,3] = "X"
    #println("Selectable candidates (chosen by faculty): ", row[FacTA_student[course]])
    students = split(row[FacTA_student[course]],",")
    # @show students
    #println("Student Choices (Name, PriorityRank)")
    for a in keys(StuClass)
        matchedStudent = filter(df -> df[StuClass[a]]  == row[FacTA_class[course]], dropmissing(df_students, StuClass[a]))
        for i=1:size(matchedStudent,1)
            sName = matchedStudent[!,:"Your Name"][i]
            matches, _ = matchNames(sName,students)
            #@show sName

            if matches>1
                if df_classes[ind_matched,4] == ""
                    df_classes[ind_matched,4] = df_classes[ind_matched,4] *  sName
                else
                    df_classes[ind_matched,4] = df_classes[ind_matched,4] *  ", " * sName 
                end

                #println(sName, " ", matchedStudent[!,StuInterest[a]][i], ", NameMatches: ", matches)
            else
                if df_classes[ind_matched,5] == ""
                    df_classes[ind_matched,5] = df_classes[ind_matched,5]  * sName
                else
                    df_classes[ind_matched,5] = df_classes[ind_matched,5]  * ", " * sName
                end

            end
        end
    end
end

"find matches for students (for a class, with faculty input missing)"
function find_matched_students(df_classes, df_students, class)
    ind_matched = findall(df_classes."Class Name".==class)[1]
    #println(ind_matched)
    df_classes[ind_matched,3] = "O"
    
    for a in keys(StuClass)
        matchedStudent = filter(df -> df[StuClass[a]]  == class, dropmissing(df_students, StuClass[a]))
        for i=1:size(matchedStudent,1)
            sName = matchedStudent[!,:"Your Name"][i]
            if df_classes[ind_matched,5] == ""
                df_classes[ind_matched,5] = df_classes[ind_matched,5]  * sName
            else
                df_classes[ind_matched,5] = df_classes[ind_matched,5]  * ", " * sName 
            end
        end
    end
end
