library(data.table)
library(mltools)
library(dplyr)
library(tidyr)
library(here)
library(ggplot2)
library(ggdendro)
library(factoextra)
set.seed(1234)
data <- data.frame(read.csv(file.path(here(), "data", "02_preprocessed_data.csv"),
    header = TRUE
), stringsAsFactors = TRUE)

colnames(data)[5:ncol(data)] <- c(
    "1AC-OUVERTURE", "1AC-MI",
    "2A-UEchoix-S1-UC1", "2A-UEchoix-S1-UC2", "2A-UEchoix-S2-UC3",
    "2A-UEchoix-S2-UC4",
    "2A-UEchoix-S2-UC5", "2A-UEchoix-S2-UC6",
    "2A-Projet-S2"
)

data <- data %>%
    mutate_if(sapply(data, is.character), as.factor)

selected_cols <- c(
    "dominante3A", "parcours", "domaine2A", "1AC-MI",
    "2A-UEchoix-S1-UC1", "2A-UEchoix-S1-UC2", "2A-UEchoix-S2-UC4",
    "2A-UEchoix-S2-UC3", "2A-UEchoix-S2-UC5", "2A-UEchoix-S2-UC6",
    "2A-Projet-S2", "1AC-OUVERTURE"
)

onehot_data <- one_hot(as.data.table(data), cols = selected_cols, sparsifyNAs = TRUE)

#  Fonctionne bien avec binary
dist_eucl <- dist(x = onehot_data[, - 1], method = "binary")

hclust_avg <- hclust(dist_eucl, method = "average")

dhc <- as.dendrogram(hclust_avg)

plotdata <- dendro_data(dhc, type = "rectangle")
p <- ggplot(segment(plotdata)) +
    geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
    coord_flip() +
    scale_y_reverse(expand = c(0.2, 0))
p + theme_dendro()

fviz_nbclust(onehot_data[, -c(1, 2, 3)], FUNcluster = hcut, k.max = 30, method = "wss")

cut_avg <- cutree(hclust_avg, k = 6)
names(cut_avg) <- data[["ine"]]
table(cut_avg)

data[["cluster"]] <- cut_avg

write.csv(data, file.path(here(), "data", "03_cah_results.csv"), row.names = FALSE)
