function defineStructure() {}

function onSync(lastSyncDate) {}

function createDataset(fields, constraints, sortFields) {
    var dataset = DatasetBuilder.newDataset();

    dataset.addColumn("codigoCentroCusto");
    dataset.addColumn("centroCusto");
    dataset.addColumn("gestorCentroCusto");
    dataset.addColumn("idGestor");
    dataset.addColumn("emailGestor");
    dataset.addColumn("diretorCentroCusto");
    dataset.addColumn("idDiretor");
    dataset.addColumn("diretorGeral");
    dataset.addColumn("idDiretorGeral");

    var dados = [
        { cod: "41101", cc: "Comercial Alagoas",         gestor: "Raquel Tenorio",       diretor: "Analanny Magalhães",     dg: "João Marcelo" },
        { cod: "41101", cc: "Comercial Bahia",            gestor: "Arianne Sodre",        diretor: "Analanny Magalhães",     dg: "João Marcelo" },
        { cod: "41101", cc: "Comercial Sergipe",          gestor: "Iolanda Bezerra",      diretor: "Analanny Magalhães",     dg: "João Marcelo" },
        { cod: "41101", cc: "Marketing e Treinamento",    gestor: "Andreia Leite",        diretor: "João Marcelo",           dg: "João Marcelo" },
        { cod: "51101", cc: "Diretoria e Gestão",         gestor: "João Marcelo",         diretor: "Diogo Demetrio",         dg: "João Marcelo" },
        { cod: "51102", cc: "Operações",                  gestor: "Diogo Demetrio",       diretor: "Ariklenyo Nascimento",   dg: "João Marcelo" },
        { cod: "51103", cc: "Controladoria",              gestor: "Ariklenyo Nascimento", diretor: "Marla Palma",            dg: "João Marcelo" },
        { cod: "51104", cc: "Compliance",                 gestor: "Marla Palma",          diretor: "Lucila Brandão",         dg: "João Marcelo" },
        { cod: "51107", cc: "Administrativo",             gestor: "Lucila Brandão",       diretor: "Francine Peiter",        dg: "João Marcelo" },
        { cod: "51110", cc: "Gente e Cultura",            gestor: "Francine Peiter",      diretor: "Igor Oliveira",          dg: "João Marcelo" },
        { cod: "51113", cc: "TI",                         gestor: "Igor Oliveira",        diretor: "Lucas Rocha",            dg: "João Marcelo" },
        { cod: "51114", cc: "Projetos e Performance",     gestor: "Lucas Rocha",          diretor: "Andrey Cunha",           dg: "João Marcelo" }
    ];

    function getColleagueById(nome) {
        if (!nome) return { id: "", email: "" };
        try {
            var c1 = DatasetFactory.createConstraint("colleagueName", nome, nome, ConstraintType.MUST);
            var ds = DatasetFactory.getDataset("colleague", ["colleagueId", "mail"], [c1], null);
            if (ds && ds.rowsCount > 0) {
                return {
                    id:    String(ds.getValue(0, "colleagueId") || ""),
                    email: String(ds.getValue(0, "mail") || "")
                };
            }
        } catch (e) {}
        return { id: "", email: "" };
    }

    for (var i = 0; i < dados.length; i++) {
        var d       = dados[i];
        var gestor  = getColleagueById(d.gestor);
        var diretor = getColleagueById(d.diretor);
        var dg      = getColleagueById(d.dg);

        dataset.addRow([
            d.cod, d.cc,
            d.gestor, gestor.id, gestor.email,
            d.diretor, diretor.id,
            d.dg, dg.id
        ]);
    }

    return dataset;
}

function onMobileSync(user) {}
