<commit_before>require 'date'
require_relative 'google_api_authorization'
require_relative '../mailers/scheduler_mailer'

NEW_SCHEDULE = "new_schedule"
CANCELLATION = "cancellation"

if not ENV['TESTING_ENV']
  $service = Google::Apis::SheetsV4::SheetsService.new
  $service.client_options.application_name = APPLICATION_NAME
  $service.authorization = authorize
end

def fetch_month_sheets()
    for day in ('1'..'2').to_a
        range = day + "!" + ENV["CELL_RANGE"]
        info_list = get_sheet_response(range)
        detect_change_send_email(info_list)
    end 
end

def get_sheet_response(range)
    $service.get_spreadsheet_values(ENV["SPREADSHEET_ID"], range).values # mock this
end

def detect_change_send_email(info_list)
    str_date = info_list[0][0].split(" ")[0].split("/")
    date = Date.new(str_date[2].to_i,str_date[0].to_i,str_date[1].to_i)
    for row in info_list[3..-1]
        if not Timeslot.find_by_date_and_time(date,row[0]).nil? and \
            not Timeslot.find_by_date_and_time(date,row[0])[:client_name].nil? and \
            row.length == 2
            
            ca_id = Ca.find_by_name(row[1].downcase)[:id]
            # send cancellation email
            SchedulerMailer.send_email(ca_id, CANCELLATION).deliver_now
        elsif not Timeslot.find_by_date_and_time(date,row[0]).nil? and \
            Timeslot.find_by_date_and_time(date,row[0])[:client_name].nil? and \
            row.length == 6
            
            # this will error out if ca name is not right
            ca_id = Ca.find_by_name(row[1].downcase)[:id]
            # find timeslot
            ts = Timeslot.find_by_date_and_time_and_ca_id(date, row[0], ca_id)
            ts.update({
                    	:client_name => row[2].downcase,
                    	:phone_number => row[3],
                    	:apt_number => row[4],
                    	:current_tenant => row[5]
            })
            # send new schedule notification email
            SchedulerMailer.send_email(ca_id, NEW_SCHEDULE).deliver_now
        end
    end
    return false
    
end

# for CA putting their availability
def write_to_spreadsheet(row, name, email)
    requests = []
    requests.push({
    update_cells: {
      start: {sheet_id: 0, row_index: row, column_index: 0},
      rows: [
        {
          values: [
            {
                user_entered_value: {string_value: name}
            }
          ]
        }
      ],
      fields: 'userEnteredValue'
    }
    })
    requests.push({
    update_cells: {
    start: {sheet_id: 0, row_index: row, column_index: 2},
    rows: [
      {
        values: [
          {
            user_entered_value: {string_value: email}
          }
        ]<commit_msg>update ts when there is a cancellation<commit_after>require 'date'
require_relative 'google_api_authorization'
require_relative '../mailers/scheduler_mailer'

NEW_SCHEDULE = "new_schedule"
CANCELLATION = "cancellation"

if not ENV['TESTING_ENV']
  $service = Google::Apis::SheetsV4::SheetsService.new
  $service.client_options.application_name = APPLICATION_NAME
  $service.authorization = authorize
end

def fetch_month_sheets()
    for day in ('1'..'2').to_a
        range = day + "!" + ENV["CELL_RANGE"]
        info_list = get_sheet_response(range)
        detect_change_send_email(info_list)
    end 
end

def get_sheet_response(range)
    $service.get_spreadsheet_values(ENV["SPREADSHEET_ID"], range).values # mock this
end

def detect_change_send_email(info_list)
    str_date = info_list[0][0].split(" ")[0].split("/")
    date = Date.new(str_date[2].to_i,str_date[0].to_i,str_date[1].to_i)
    for row in info_list[3..-1]
        if not Timeslot.find_by_date_and_starttime(date,row[0]).nil? and \
            not Timeslot.find_by_date_and_starttime(date,row[0])[:client_name].nil? and \
            row.length == 2
            
            ca_id = Ca.find_by_name(row[1].downcase)[:id]
            
            # find timeslot
            ts = Timeslot.find_by_date_and_starttime_and_ca_id(date, row[0], ca_id)
            # update timeslot
            update_ts(ts, nil, nil, nil, nil)
            
            # send cancellation email
            SchedulerMailer.send_email(ca_id, CANCELLATION).deliver_now
        elsif not Timeslot.find_by_date_and_starttime(date,row[0]).nil? and \
            Timeslot.find_by_date_and_starttime(date,row[0])[:client_name].nil? and \
            row.length == 6
            
            # this will error out if ca name is not right
            ca_id = Ca.find_by_name(row[1].downcase)[:id]
            
            # find timeslot
            ts = Timeslot.find_by_date_and_starttime_and_ca_id(date, row[0], ca_id)
            # update timeslot
            update_ts(ts, row[2].downcase, row[3], row[4], row[5])

            # send new schedule notification email
            SchedulerMailer.send_email(ca_id, NEW_SCHEDULE).deliver_now
        end
    end
    return false
end

def update_ts(ts, client_name, phone_number, apt_number, current_tenant)
  ts.update({
          	:client_name => client_name,
          	:phone_number => phone_number,
          	:apt_number => apt_number,
          	:current_tenant => current_tenant
  })
end

# for CA putting their availability
def write_to_spreadsheet(row, name, email)
    requests = []
    requests.push({
    update_cells: {
      start: {sheet_id: 0, row_index: row, column_index: 0},
      rows: [
        {
          values: [
            {
                user_entered_value: {string_value: name}
            }
          ]
        }
      ],
      fields: 'userEnteredValue'
    }
    })
    requests.push({
    update_cells: {
    start: {sheet_id: 0, row_index: row, column_index: 2},
    rows: [
      {
        values: [
          {
            user_entered_value: {string_value: email}
          }
        ]
{'fraction_non_alphanumeric': 0.10987315735344531, 'fraction_numerical': 0.009770311964346932, 'mean_word_length': 2.324786324786325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '488322', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1932, 'n_words': 343}