# Assumes you have https://github.com/tirandagan/fccULSloader installed and 
# updated somewhere

library(dbplyr)
library(RSQLite)
library(dplyr)
library(tidyr)
library(janitor)
library(USAboundaries)

# Update local database
system("cd python3 fccULSloader/src/fcc_tool.py --update")

# Get current date
now <- Sys.Date()

con <- dbConnect(SQLite(), "fccULSloader/src/data/fcc_data.db")

am <- tbl(con, "AM")
en <- tbl(con, "EN")

a_raw <- dbGetQuery(con, "SELECT COUNT(*) FROM EN LEFT JOIN HD ON 
                    EN.unique_system_identifier = HD.unique_system_identifier 
                    WHERE license_status = 'A';")

total_active <- a_raw %>% rename(count = `COUNT(*)`) %>% 
                        mutate(state_name = "TOTAL")

print(total_active)

# Query active licenes by state 
b_raw <- dbGetQuery(con, "SELECT UPPER(state), COUNT(*) FROM EN LEFT JOIN HD ON 
                    EN.unique_system_identifier = HD.unique_system_identifier 
                    WHERE license_status = 'A'
                    GROUP BY UPPER(state);") 

active_by_state <- b_raw %>%
    left_join(state_codes %>% filter(state_abbr != ""), 
              by = c("UPPER(state)" = "state_abbr")) %>% 
    rename(abbrev = `UPPER(state)`,
                      count = `COUNT(*)`) %>% 
        mutate(state_name = case_when(abbrev == "AA" ~ "Armed Forces America",
                                      abbrev == "AE" ~ "Armed Forces Europe",
                                      abbrev == "AP" ~ "Armed Forces Pacific",
                                      abbrev == "VI" ~ "Virgin Islands",
                                      abbrev == "UM" ~ "US Minor Islands",
                                      abbrev == ""   ~ "Other*",
                                      TRUE ~ state_name)) %>% 
    bind_rows(total_active)

print(active_by_state)

# Query active licenes by state, by class
d_raw <- dbGetQuery(con, "SELECT UPPER(state), operator_class, COUNT(*) FROM EN LEFT JOIN HD ON 
                    EN.unique_system_identifier = HD.unique_system_identifier LEFT JOIN AM ON
                    EN.unique_system_identifier = AM.unique_system_identifier 
                    WHERE license_status = 'A'
                    GROUP BY UPPER(state), operator_class;") 

active_state_class <- d_raw %>%
    left_join(state_codes %>% filter(state_abbr != ""), 
              by = c("UPPER(state)" = "state_abbr")) %>% 
    rename(abbrev = `UPPER(state)`,
           count = `COUNT(*)`) %>% 
    mutate(state_name = case_when(abbrev == "AA" ~ "Armed Forces America",
                                  abbrev == "AE" ~ "Armed Forces Europe",
                                  abbrev == "AP" ~ "Armed Forces Pacific",
                                  abbrev == "VI" ~ "Virgin Islands",
                                  abbrev == "UM" ~ "US Minor Islands",
                                  abbrev == ""   ~ "Other*",
                                  TRUE ~ state_name)) %>% 
    #bind_rows(total_active) %>% 
    select(-state_code, -abbrev, -jurisdiction_type) %>% 
    mutate(operator_class = case_when(operator_class == "A" ~ "Advanced",
                                      operator_class == "E" ~ "Extra",
                                      operator_class == "G" ~ "General",
                                      operator_class == "N" ~ "Novice",
                                      operator_class %in% c("T","P") ~ "Tech")) %>% 
    pivot_wider(names_from = operator_class,
                values_from = count) %>% 
    mutate(Date = now) %>% 
    rename("State/Territory" = state_name) %>% 
    select(Date, `State/Territory`, Novice, Tech, General, Advanced, Extra, `NA`) %>% 
    mutate("Tech-Plus" = NA,
           .before = General) %>% 
    rowwise() %>% mutate("Total" = sum(c_across(Novice:`NA`),
                                       na.rm = T)) %>% 
    select(-`NA`) %>% 
    group_by(Date) %>% 
    bind_rows(summarise(., across(where(is.numeric), sum, na.rm = T),
                        across(where(is.character), ~'TOTAL'))) %>% 
    mutate(Tech_and_TechPlus = NA,
           Conditional = NA,
           Club	= NA,
           Military = NA,
           Multiple	= NA,
           Repeater	= NA,
           GMRS	= NA,
           source_name	= "fccULSloader",
           source_detail = "w1cdn.net"
)

# Append to table
write.table(active_state_class, file = "out/fccULSloader-fcc-licenses-scraped.csv", sep = ",", 
            append = TRUE, quote = FALSE, 
            col.names = F, row.names = FALSE,
            na = "") 




