package leigh.esp.goryu.sdk;

import leigh.eo.EO;
import leigh.eo.EODataType;
import leigh.eo.schema.DAOGenerator;
import leigh.eo.schema.dao.*;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.UniversalJob;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * Schema generator for the Goryu SDK.
 *
 * @author Alex Leigh
 */
public class GoryuSchemaGeneratorApp {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(GoryuSchemaGeneratorApp.class);

    public static EO generate() {
        final EO schema = SchemaUtil.create(SchemaUtil.API_KEY);

        final EO scalar = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(scalar);
        ElementElementDAO.setApiName(scalar, "leigh.esp.goryu.v1.Scalar");
        ElementElementDAO.getFields(scalar).add(LoopUtil.create("value", "Value"));

        final EO data = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(data);
        ElementElementDAO.setApiName(data, "leigh.esp.goryu.v1.Data");
        // This array holds the values that match up to the place-holders in the SQL query
        ElementElementDAO.getFields(data).add(LoopUtil.create("values", "Values"));
        // Note: This object is dynamic (may contain other fields) but we have no way to represent that in the
        // schema generator today.

        final EO query = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(query);
        ElementElementDAO.setApiName(query, "leigh.esp.goryu.v1.Query");
        ElementElementDAO.getFields(query).add(FieldUtil.createNotNull("query", "Query Text"));

        final EO mutation = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(mutation);
        ElementElementDAO.setApiName(mutation, "leigh.esp.goryu.v1.Mutation");
        ElementElementDAO.getFields(mutation).add(FieldUtil.createNotNull("query", "Query Text"));

        final EO subStreamingMutation = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(subStreamingMutation);
        ElementElementDAO.setApiName(subStreamingMutation, "leigh.esp.goryu.v1.SubStreamingMutation");
        ElementElementDAO.getFields(subStreamingMutation).add(FieldUtil.createNotNull("query", "Query Text"));
        ElementElementDAO.getFields(subStreamingMutation).add(FieldUtil.create("generatedKeyName", "Generated Key Name"));
        ElementElementDAO.getFields(subStreamingMutation).add(FieldUtil.createNotNull("dataAttributeName", "Data Attribute Name"));
        ElementElementDAO.getFields(subStreamingMutation).add(LoopUtil.create("subQueries", "SubQueries"));

        final EO streamingMutation = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(streamingMutation);
        ElementElementDAO.setApiName(streamingMutation, "leigh.esp.goryu.v1.StreamingMutation");
        ElementElementDAO.getFields(streamingMutation).add(FieldUtil.createNotNull("query", "Query Text"));
        ElementElementDAO.getFields(streamingMutation).add(FieldUtil.create("generatedKeyName", "Generated Key Name"));
        ElementElementDAO.getFields(streamingMutation).add(FieldUtil.create("hasData", "Generated Key Name", EODataType.bool));
        ElementElementDAO.getFields(streamingMutation).add(LoopUtil.create("subQueries", "SubQueries"));

        return schema;
    }

    public static void main(final String... args) throws FileNotFoundException {
        UniversalJob.banner(logger, "Apotheosis mk3", "2014 Alex Leigh");
        final EO schema = generate();
        final DAOGenerator gen = new DAOGenerator(new File("leigh-esp-goryu-sdk/src/main/java"));
        gen.generateJava("leigh.esp.goryu.sdk.schema.v1", schema);

        logger.info("Generated schema: {}", schema);
    }
}
