# ---------------------------
#
# Script name: clean_ui.R
#
# Purpose of script: Describes the Ui of the cleaning tab
# Author: Daniela Lührsen
# Date Created: 2026-03
# Email: daniela.luhrsen@bsc.es
#
# ---------------------------

clean_ui <- function(id) {
  ns <- NS(id)
  tabPanel(

    title = "Clean",
    value = ns("tab"),

    tags$br(),

    ## Load data -------------------------------------------------------------
    h3("Upload your data here"),
    p("Accepted file formats are .csv, .dbf, .dbc, .rds, .xlsx, .xls, .json"),
    p("File should be in linelist format, each row represents a disease case ",
      "and each column is a variable describing the case."),
    fluidRow(
      column(
        width = 6,
        fileInput(ns("pre_file_input"), "",
                  accept = c(".csv", ".dbf", ".dbc", ".rds",
                             ".xlsx", ".xls", ".json"),
                  multiple = TRUE)
      ),
      column(
        width = 6,
        tags$br(),
        actionButton(ns("pre_default_input"), "Load Test data")
      )
    ),

    tags$hr(),

    ## column name meaning ---------------------------------------------------
    h3("Column names"),
    tags$hr(),

    tags$img(src = "eye.png", width = "20"),
    "In your current dataset you have the following columns:",
    shiny::uiOutput(ns("pre_colnames")),
    p(HTML(paste0("If you are using official data from one of the project
                  countries, you can find the definition of the column names
                  in one of the dictionaries which can be found in the ",
                  actionLink(ns("go_documentation"), "Documentation"),
                  " tab."))),
    ### add files here
    ## Include Columns -------------------------------------------------------

    tags$hr(),

    tags$img(src = "filter.png", width = "20"),
    "Often we are not interested in all of these columns, but only in a few of
    them. Select which columns you want to keep for your analysis:",
    tags$br(),
    tags$br(),

    uiOutput(ns("pre_include_columns_ui")),
    tags$br(),
    uiOutput(ns("pre_include_columns_uicode")),
    actionButton(ns("pre_include_columns_button"), "Filter columns"),

    ## rename columns --------------------------------------------------------

    tags$hr(),
    tags$img(src = "filter.png", width = "20"),
    "Now that you only have the column that you would like to work with. Let's
    give them names that make sense to you. Again, If your data is from one of
      HARMONIZE project countries, we will give you suggestions. Feel free to 
    change them.\n \nAvoid empty spaces, as these can cause errors.",
    tags$br(),
    tags$br(),
    tags$br(),

    shiny::uiOutput(ns("pre_rename_ui")),
    shiny::uiOutput(ns("pre_rename_uicode")),
    tags$hr(),
    ## completeness of data --------------------------------------------------
    h3("Completeness of the data"),

    tags$img(src = "idea.png", width = "20"),
    "Some of these variables are mandatory to notify, others are not, which
      leads to incomplete datasets. Below you can see which columns are
      complete and a chart showing the variables which have NA-values and the
      percentage of NAs they contain.",
    tags$br(),
    tags$br(),

    tags$img(src = "eye.png", width = "20"),
    "In the plot below, each bar on the x-axis represents a column, and the 
    bar height shows the percentage of missing values.",
    tags$br(),
    tags$br(),
    uiOutput(ns("pre_plot_missing_uicode")),
    plotlyOutput(ns("NAplot")),
    tags$br(),
    ## remove columns with missing values ------------------------------------

    tags$hr(),

    tags$img(src = "filter.png", width = "20"),
    "If some of the variables are more empty than you would like to can set a
    threshold value, and any variable with more missing values than your 
    threshold will be removed. If you want only complete variables you can set
      the threshold to 0.",
    tags$br(),
    tags$br(),
    numericInput(ns("pre_remove_threshold"), label = "Threshold value:",
                 value = 80, min = 0, max = 100),
    uiOutput(ns("pre_remove_missing_ui")),
    actionButton(ns("pre_remove_missing_button"), "Delete columns"),

    ## rename categories -----------------------------------------------------
    tags$hr(),
    h3("Rename categories"),

    tags$img(src = "idea.png", width = "20"),
    "Variables are often categorized into for simpler input. (e.g. 1 --> 
    Presents fever, 2 --> No Fever), or are given abbreviations (e.g. f --> 
    female, m--> male). To understand your data better it is often useful to 
    change these values to something meaningful to you.",
    tags$br(),
    tags$br(),

    tags$img(src = "filter.png", width = "20"),
    "Here you can select a variables whose categories you wish to 
    rename. You can only select variables that have 20 or less unique 
    values.",
    tags$br(),
    tags$br(),
    selectInput(inputId = ns("pre_rename_categories"),
                label = "Select a column",
                choices = c()),
    uiOutput(ns("pre_rename_categories_ui")),
    uiOutput(ns("pre_rename_categories_codeui")),
    actionButton(ns("pre_rename_categories_button"), "Rename categories"),

    ## add new temporal resolutions ------------------------------------------
    tags$hr(),
    h3("Add new temporal resolutions"),
    tags$br(),
    tags$br(),
    "The userinterface currently does not allow for 'week to date'.",
    selectInput(inputId = ns("pre_week_start"), "Select your start day:",
                choices = c("Monday" = "Monday",
                            "Tuesday" = "Tuesday",
                            "Wednesday" = "Wednesday",
                            "Thursday" = "Thursday",
                            "Friday" = "Friday",
                            "Saturday" = "Saturday",
                            "Sunday" = "Sunday"),
                selected = "Monday"),
    fluidRow(
      column(
        width = 4,
        selectInput(inputId = ns("pre_temporal_date"),
                    label = "Select your date column",
                    choices = c())
      ),
      column(
        width = 8,
        checkboxGroupInput(inputId = ns("pre_temporal_choices"),
                           label = "What temporal resolution do you want?",
                           choices = c("Week (Week number)" = "weeknumber",
                                       "Week (Date of the first day of the week)" = "weekdate",
                                       "Month (Month number)" = "monthnumber",
                                       "Month (Date of the first day of the month)" = "monthdate",
                                       "Year" = "yearnumber"),
                           inline = TRUE)
      )
    ),
    uiOutput(ns("pre_temporal_codeui")),
    actionButton(ns("pre_temporal_button"), "Add temporal resolutions"),
    tags$br(),

    ## finishing up: download and continue -----------------------------------
    tags$br(),
    fluidRow(
      column(
        width = 6,
        downloadButton(ns("pre_download_clean"),
                       label = "Download Cleaned Data")
      ),
      column(
        width = 6,
        actionButton(ns("go_clean_filter"),
                     "Continue with clean data to filter")
      )
    ),
    tags$br(),
    tags$br()
  )
}

### END
