package leigh.tansu.sdk.v1;

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

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

import static leigh.eo.schema.dao.SchemaUtil.API_KEY;
import static leigh.eo.schema.dao.SchemaUtil.create;

public class SchemaGeneratorApp {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(SchemaGeneratorApp.class);

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

        final EO objectGetRequest = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(objectGetRequest);
        ElementElementDAO.setApiName(objectGetRequest, "leigh.tansu.sdk.v1.ObjectGetRequest");
        ElementElementDAO.getFields(objectGetRequest).add(
                FieldUtil.createNotNull("bucketId", "Bucket ID", EODataType.string));
        ElementElementDAO.getFields(objectGetRequest).add(
                FieldUtil.createNotNull("id", "Object ID", EODataType.string));

        final EO objectPutResponse = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(objectPutResponse);
        ElementElementDAO.setApiName(objectPutResponse, "leigh.tansu.sdk.v1.ObjectPutResponse");

        final EO objectPutRequest = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(objectPutRequest);
        ElementElementDAO.setApiName(objectPutRequest, "leigh.tansu.sdk.v1.ObjectPutRequest");
        ElementElementDAO.getFields(objectPutRequest).add(
                FieldUtil.createNotNull("bucketId", "Bucket ID", EODataType.string));
        ElementElementDAO.getFields(objectPutRequest).add(
                FieldUtil.createNotNull("id", "Object ID", EODataType.string));

        final EO bucketCreateResponse = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(bucketCreateResponse);
        ElementElementDAO.setApiName(bucketCreateResponse, "leigh.tansu.sdk.v1.BucketCreateResponse");

        final EO bucketCreateRequest = ElementElementDAO.create();
        SchemaElementDAO.getElements(schema).add(bucketCreateRequest);
        ElementElementDAO.setApiName(bucketCreateRequest, "leigh.tansu.sdk.v1.BucketCreateRequest");
        ElementElementDAO.getFields(bucketCreateRequest).add(
                FieldUtil.createNotNull("id", "Bucket ID", EODataType.string));

        return schema;
    }

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

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