#!/usr/bin/env python3
import click
import logging
import lflex_celcat_survival as lcs

@click.command()
@click.argument('course_request_file')
@click.argument('credentials_file')
@click.option('--json', '--json-raw', default=None, help='If set, raw CELCAT events are written as JSON to this file.')
@click.option('--csv-raw', default=None, help='If set, raw (unfiltered) events are written as CSV to this file.')
@click.option('--csv', default=None, help='If set, filteret events are written as CSV to this file.')
@click.option('--ics', default=None, help='If set, filtered events are written as ICS to this file.')
@click.option('--csv-no-description', is_flag=True, default=False, help='If set, CSV outputs will not contain the description column.')
def main(course_request_file, credentials_file, json, csv_raw, csv, ics, csv_no_description):
    logging.basicConfig(level=logging.INFO)

    #req = lcs.course_request.CourseRequest(course_request_file)
    #if all(o is None for o in [json, csv_raw, csv, ics]):
    #    logging.warning('No option set, doing nothing.')
    #    return

    username, password, teacher_code = lcs.auth.parse_credentials_from_file(credentials_file)
    session = lcs.auth.create_authenticated_session(username, password)

    requested_slots_df = lcs.slot_parse.read_weekslot_csv(course_request_file, 'fr', 2)
    celcat_slots = lcs.events.request_slots_by_mod_code(requested_slots_df, session)

    # slots listed in entry file but absent from celcat
    print(celcat_slots[celcat_slots['slot_in_celcat'].isna()])

    # slots listed in entry file and on celcat, but with no reserved room
    print(celcat_slots[celcat_slots['room_parsed'] == 'unset'])

    return

    celcat_raw_response = req.do_request(session)
    if json is not None:
        with open(json, 'w') as f:
            f.write(celcat_raw_response)
    if all(o is None for o in [csv_raw, csv, ics]):
        return

    csv_columns_to_drop = []
    if csv_no_description:
        csv_columns_to_drop = ['description']

    celcat_events = lcs.events.CelcatEvents(celcat_raw_response)
    if csv_raw is not None:
        celcat_events.df.drop(columns=csv_columns_to_drop).to_csv(csv_raw, index=False)
    if all(o is None for o in [csv, ics]):
        return

    filtered_celcat_events = lcs.events.FilteredCelcatEvents(req, celcat_events)
    filtered_celcat_events.check_expected_nb_timeslots()
    if csv is not None:
        filtered_celcat_events.df.drop(columns=csv_columns_to_drop).to_csv(csv, index=False)
    if all(o is None for o in [ics]):
        return

    calendar = lcs.ics.course_df_to_ics(filtered_celcat_events.df)
    if ics is not None:
        with open(ics, 'w') as f:
            f.write(str(calendar))

if __name__ == "__main__":
    main()