import com.fasterxml.jackson.databind.ObjectMapper;
import com.recombee.api_client.RecombeeClient;
import com.recombee.api_client.api_requests.*;
import com.recombee.api_client.bindings.BatchResponse;
import com.recombee.api_client.exceptions.ApiException;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/*
Structure of the .json with item catalog:
{
    "nyphilharmonic181210": {
        "title": "New York Philharmonic: Rachmaninoff",
        "city": "New York",
        "venue": "Lincoln Center",
        "date":  "2017-12-10",
        "genres": ["classical music"],
        "price": 94.0
    }
    "beachboys190321": { ... },
    ....
}
*/


public class Main {

    public static void main(String[] args) throws ApiException {

        RecombeeClient client = new RecombeeClient("events-example", "PbBaEVxx8ZOj0x3BhGtqfHyi8qQ8rm8rE1JBnSPoCnHwetzO3gjHer96YVAIa14G");

        client.send(new ResetDatabase());
        client.send(new AddItemProperty("title", "string"));
        client.send(new AddItemProperty("city", "string"));
        client.send(new AddItemProperty("venue", "string"));
        client.send(new AddItemProperty("date", "timestamp"));
        client.send(new AddItemProperty("genres", "set"));
        client.send(new AddItemProperty("price", "double"));

        ArrayList<Request> requests = new ArrayList<>();

        try {
            // HashMap<itemId, HashMap<name of property, value of property>>
            HashMap<String, HashMap<String, Object>> items =
                    new ObjectMapper().readValue(new FileReader("items.json"), HashMap.class);
            for (Map.Entry<String, HashMap<String, Object>> entry : items.entrySet()) {
                String itemId = entry.getKey();
                HashMap<String, Object> values = entry.getValue();
                requests.add(new SetItemValues(itemId, values).setCascadeCreate(true));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        BatchResponse[] res = client.send(new Batch(requests));
    }
}