@compiler >= 6

main contract Schedules =
    record schedule =
        { id : string, cron: string, feature_hash: string}
    record state =
        { owner      : address,
          schedules  : map(address, map(string, schedule)) }

    stateful entrypoint init() : state =
        { owner = Call.caller,
          schedules = {} }


    stateful entrypoint add_schedule(id: string, feature_hash: string, cronspec: string) =
        let oldmap = Map.lookup_default(Call.caller, state.schedules, {})
        let newmap = oldmap{[id] = {id = id, cron = cronspec, feature_hash = feature_hash}}
        put(state{schedules[Call.caller] = newmap})

    stateful entrypoint delete_schedule(id: string) =
        let oldmap = Map.lookup_default(Call.caller, state.schedules, {})
        let newmap = Map.delete(id, oldmap)
        put(state{schedules[Call.caller] = newmap})

    stateful entrypoint get_schedules(): map(string, schedule) =
        Map.lookup_default(Call.caller, state.schedules, {})

    stateful entrypoint get_all_schedules(): map(address, map(string, schedule)) =
        require(Call.caller == state.owner, "only_owner_call_allowed")
        state.schedules
