apiVersion: v1
kind: ConfigMap
metadata:
  name: healthform-jobscript
  namespace: healthform
  labels:
    app: healthform
data:
  job.py: |
    import argparse
    import json
    import os, pathlib
    import requests

    from datetime import datetime
    from pytz import timezone
    from typing import Any, List, Literal

    def get_pwd():
      return pathlib.Path(__file__).parent.resolve()

    # define timezone to UTC+7
    tz_jkt = timezone('Asia/Jakarta')

    # read json data for payload of each shift case
    wfo_payload_file = open(os.path.join(get_pwd(), 'payload/wfo.json'), 'r')
    wfh_payload_file = open(os.path.join(get_pwd(), 'payload/wfh.json'), 'r')
    weekend_payload_file = open(os.path.join(get_pwd(), 'payload/weekend.json'), 'r')
    wfo_payload = json.loads(wfo_payload_file.read())
    wfh_payload = json.loads(wfh_payload_file.read())
    weekend_payload = json.loads(weekend_payload_file.read())

    def decide_weekend(date: datetime) -> bool:
      if date.isoweekday() == 6 or date.isoweekday() == 7:
        return True
      return False

    def decide_date(
      date: datetime,
      first_date: datetime,
      wfh_weeks: List[int],
      n_shift: int = 4
    ) -> Literal['weekend', 'wfh', 'wfo']:
      is_weekend = decide_weekend(date)
      if is_weekend:
        return 'weekend'
      
      # if current week number is mulplier of wfh_week, then it's wfh
      week_number = (date.isocalendar().week - first_date.isocalendar().week) + 1
      for wfh_week in wfh_weeks:
        if wfh_week > n_shift or wfh_week <= 0:
          raise ValueError(f'wfh_week ({wfh_week}) can\'t be bigger than total shift ({n_shift}) or lower than 0')

        if wfh_week == n_shift and week_number % n_shift == 0:
          return 'wfh'
        
        if week_number % n_shift == wfh_week:
          return 'wfh'
        
      return 'wfo'

    def create_request_payload(date_type: Literal['weekend', 'wfh', 'wfo']) -> dict[str, Any]:
      if date_type == 'wfo':
        return wfo_payload

      if date_type == 'wfh':
        return wfh_payload

      if date_type == 'weekend':
        return weekend_payload

    # parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--first_date', type=str, help='first day of the shift, ex: 2021-11-08 (format %Y-%m-%d)')
    parser.add_argument('--n_shift', type=int, help='total shift group', required=False, default=4)
    parser.add_argument('--wfh_weeks', type=int, nargs='+')
    args = parser.parse_args()

    # main process
    if __name__=='__main__':
      # define datetime of the first day of applied shift
      first_date = datetime.strptime(args.first_date, '%Y-%m-%d').astimezone(tz_jkt)
      current_date = datetime.now(tz_jkt)

      # define request payload based on wfo, wfh, or weekend
      print('deciding date type ....')
      date_type = decide_date(current_date, first_date, args.wfh_weeks, args.n_shift)
      print('date type decided: ' + date_type)

      print('generating form data ....')
      request_payload = create_request_payload(date_type)
      print('form data generated')
      print(request_payload)

      # fill the form
      print('sending form data ....')
      requests.post('http://healthform-service:5000', json=request_payload)
      print('sending form data success')