module PSBoardDataBase

using SQLite
using DBInterface
using Tables
using CSV
using DataFrames
using Dates

include("QaqcMasterLog.jl")
include("ClockParser.jl")
include("SlaveLogParser.jl")

include("create_table.jl")
include("DownloadCSVs.jl")
include("import_data.jl")

include("DispatchChecker.jl")
using .DispatchChecker

"""
    create_database_from_exported_csvs(
        dbpath::AbstractString;
        single_run_csv::AbstractString = DownloadCSVs.download_single_run_csv(),
        runlist_csv::AbstractString = DownloadCSVs.download_runlist_csv(),
        dispatch_csv::AbstractString = DownloadCSVs.download_dispatch_csv(),
        hundred_csv::AbstractString = DownloadCSVs.download_hundred_run_csv(),
        jathubs_csv::AbstractString = DownloadCSVs.download_jathub_csv(),
        masterlog_dir::AbstractString,
        slavelog_dir::AbstractString,
    )

Create database at `dbpath` and import data from CSV and master log files.

# Arguments

## Required
- `dbpath`: where the database will be created
- `masterlog_dir`: path to the directory (`log`) where all JATHub master logs are stored
- `slavelog_dir`: path to the directory where all JATHub slave logs are stored

## Optional
- `single_run_csv`: CSV of single run results exported from the Google sheets database
- `runlist_csv`: CSV of run lists exported from the Google sheets database
- `dispatch_csv`: CSV of dispatch lists exported from the Google sheets database
- `hundred_csv`: CSV of 100 tests results exported from the Google sheets database
- `jathubs_csv`: CSV for jathub list used in QAQC. Used to add skew.
"""
function create_database_from_exported_csvs(
    dbpath::AbstractString;
    masterlog_dir::AbstractString,
    slavelog_dir::AbstractString,
    kw...
)
    db = create_database(dbpath)

    single_result_df,
    runlist_table,
    dispatch_table,
    extra_100test_result_df,
    jathubs_table =
        map((
            (:single_run_csv, DownloadCSVs.download_single_run_csv),
            (:runlist_csv, DownloadCSVs.download_runlist_csv),
            (:dispatch_csv, DownloadCSVs.download_dispatch_csv),
            (:hundred_csv, DownloadCSVs.download_hundred_run_csv),
            (:jathubs_csv, DownloadCSVs.download_jathub_csv),
        )) do (key, dlfunc)
            if !haskey(kw, key)
                Channel{DataFrame}(1) do ch
                    csvfile = dlfunc()
                    df = CSV.read(csvfile, DataFrame)
                    put!(ch, df)
                end
            else
                Channel{DataFrame}(1) do ch
                    put!(ch, CSV.read(kw[key], DataFrame))
                end
            end
        end .|> first

    insert_version_info(db)
    insert_qaqc_campaign_id(db)
    insert_qaqc_positions(db, jathubs_table)

    add_psboard_ids(db, single_result_df)
    add_qaqc_runlist_from_runlist(db, runlist_table)
    add_qaqc_single_result(db, single_result_df, runlist_table)
    add_qaqc_dispatch(db, dispatch_table)
    add_qaqc_runlist_from_masterlogs(db, masterlog_dir)
    add_qaqc_100test_result(db, extra_100test_result_df)
    add_skew_from_slave_clk_logs(db, slavelog_dir)
    add_slavelog_result(db, slavelog_dir)

    db
end

greet() = print("Hello World!")

end # module PSBoardDataBase
