/*
 * pxtables.c
 *
 * Copyright (C) 2006  Joshua Hoblitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <pslib.h>
#include "pxtools.h"

#define CREATE_TABLE(createFunc) \
    if (!createFunc(config->dbh)) { \
        psError(PS_ERR_UNKNOWN, false, "dbh access failed"); \
        status = false; \
    }

#define DROP_TABLE(dropFunc) \
    if (!dropFunc(config->dbh)) { \
        psError(PS_ERR_UNKNOWN, false, "dbh access failed"); \
        status = false; \
    }

bool pxCreateTables(pxConfig *config) {
    PS_ASSERT_PTR_NON_NULL(config, false);

    bool            status = true;
#if 0
    CREATE_TABLE(expTagCounterCreateTable);
    // the counter must be initialized to 0
    if (!p_psDBRunQuery(config->dbh, "INSERT INTO expTagCounter VALUES (0)")) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        status = false; 
    }
#endif
    CREATE_TABLE(pzDataStoreCreateTable);
    CREATE_TABLE(summitExpCreateTable);
    CREATE_TABLE(summitImfileCreateTable);
//    CREATE_TABLE(pzPendingExpCreateTable);
//    CREATE_TABLE(pzPendingImfileCreateTable);
//    CREATE_TABLE(pzDoneExpCreateTable);
//    CREATE_TABLE(pzDoneImfileCreateTable);
    CREATE_TABLE(pzDownloadExpCreateTable);
    CREATE_TABLE(pzDownloadImfileCreateTable);
    CREATE_TABLE(newExpCreateTable);
    CREATE_TABLE(newImfileCreateTable);
    CREATE_TABLE(rawExpCreateTable);
    CREATE_TABLE(rawImfileCreateTable);
    CREATE_TABLE(guidePendingExpCreateTable);
    CREATE_TABLE(chipRunCreateTable);
    CREATE_TABLE(chipProcessedImfileCreateTable);
    CREATE_TABLE(chipMaskCreateTable);
    CREATE_TABLE(camRunCreateTable);
    CREATE_TABLE(camProcessedExpCreateTable);
    CREATE_TABLE(camMaskCreateTable);
    CREATE_TABLE(detRunCreateTable);
    CREATE_TABLE(detInputExpCreateTable);
    CREATE_TABLE(detProcessedImfileCreateTable);
    CREATE_TABLE(detProcessedExpCreateTable);
    CREATE_TABLE(detStackedImfileCreateTable);
    CREATE_TABLE(detNormalizedStatImfileCreateTable);
    CREATE_TABLE(detNormalizedImfileCreateTable);
    CREATE_TABLE(detNormalizedExpCreateTable);
    CREATE_TABLE(detResidImfileCreateTable);
    CREATE_TABLE(detResidExpCreateTable);
    CREATE_TABLE(detRunSummaryCreateTable);
    CREATE_TABLE(detRegisteredImfileCreateTable);
    CREATE_TABLE(warpRunCreateTable);
    CREATE_TABLE(warpSkyCellMapCreateTable);
    CREATE_TABLE(warpSkyfileCreateTable);
    CREATE_TABLE(warpMaskCreateTable);
    CREATE_TABLE(stackRunCreateTable);
    CREATE_TABLE(stackInputSkyfileCreateTable);
    CREATE_TABLE(stackSumSkyfileCreateTable);
    CREATE_TABLE(diffRunCreateTable);
    CREATE_TABLE(diffInputSkyfileCreateTable);
    CREATE_TABLE(diffSkyfileCreateTable);
    CREATE_TABLE(magicRunCreateTable);
    CREATE_TABLE(magicInputSkyfileCreateTable);
    CREATE_TABLE(magicTreeCreateTable);
    CREATE_TABLE(magicNodeResultCreateTable);
    CREATE_TABLE(magicMaskCreateTable);
    CREATE_TABLE(magicSkyfileMaskCreateTable);
    CREATE_TABLE(calDBCreateTable);
    CREATE_TABLE(calRunCreateTable);
    CREATE_TABLE(flatcorrRunCreateTable);
    CREATE_TABLE(flatcorrExpCreateTable);
    CREATE_TABLE(pstampDataStoreCreateTable);
    CREATE_TABLE(pstampRequestCreateTable);
    CREATE_TABLE(pstampJobCreateTable);

    return true;
}

bool pxDeleteTables(pxConfig *config) {
    bool            status = true;
    char line[128], answer[128];

    PS_ASSERT_PTR_NON_NULL(config, false);

    // XXX use the values defined in config->dbh?
    psString dbName = psMetadataLookupStr(&status, config->modules->complete, "DBNAME");

    fprintf (stdout, "*** delete the chip tables from database %s? ***\n", dbName);
    fprintf (stdout, "*** to delete the tables, answer YES, and give password ***\n");
    fprintf (stdout, "*** WARNING: this action is permanent ***\n\n");

    fprintf (stdout, "delete the chip tables (YES/[n]): ");
    fgets (line, 128, stdin);
    sscanf (line, "%s", answer);
    if (strcmp (answer, "YES"))  {
        psError(PS_ERR_UNKNOWN, true, "tables NOT deleleted");
        return false;
    }

    fprintf (stdout, "enter dbh connection password: ");
    fgets (line, 128, stdin);
    sscanf (line, "%s", answer);

    {
        bool status;
        psString dbPassword = psMetadataLookupStr(&status, config->modules->complete, "DBPASSWORD");
        if (strcmp (answer, dbPassword)) {
            psError(PS_ERR_UNKNOWN, true, "tables NOT deleleted");
            return false;
        }
    }

    // disable foreign key constrants
    if (!p_psDBRunQuery(config->dbh, "SET FOREIGN_KEY_CHECKS=0")) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        status = false; 
    }

//    DROP_TABLE(expTagCounterDropTable);
    DROP_TABLE(pzDataStoreDropTable);
    DROP_TABLE(summitExpDropTable);
    DROP_TABLE(summitImfileDropTable);
//    DROP_TABLE(pzPendingExpDropTable);
//    DROP_TABLE(pzPendingImfileDropTable);
//    DROP_TABLE(pzDoneExpDropTable);
//    DROP_TABLE(pzDoneImfileDropTable);
    DROP_TABLE(pzDownloadExpDropTable);
    DROP_TABLE(pzDownloadImfileDropTable);
    DROP_TABLE(newExpDropTable);
    DROP_TABLE(newImfileDropTable);
    DROP_TABLE(rawExpDropTable);
    DROP_TABLE(rawImfileDropTable);
    DROP_TABLE(guidePendingExpDropTable);
    DROP_TABLE(chipRunDropTable);
    DROP_TABLE(chipProcessedImfileDropTable);
    DROP_TABLE(chipMaskDropTable);
    DROP_TABLE(camRunDropTable);
    DROP_TABLE(camProcessedExpDropTable);
    DROP_TABLE(camMaskDropTable);
    DROP_TABLE(detRunDropTable);
    DROP_TABLE(detInputExpDropTable);
    DROP_TABLE(detProcessedImfileDropTable);
    DROP_TABLE(detProcessedExpDropTable);
    DROP_TABLE(detStackedImfileDropTable);
    DROP_TABLE(detNormalizedStatImfileDropTable);
    DROP_TABLE(detNormalizedImfileDropTable);
    DROP_TABLE(detNormalizedExpDropTable);
    DROP_TABLE(detResidImfileDropTable);
    DROP_TABLE(detResidExpDropTable);
    DROP_TABLE(detRunSummaryDropTable);
    DROP_TABLE(detRegisteredImfileDropTable);
    DROP_TABLE(warpRunDropTable);
    DROP_TABLE(warpSkyCellMapDropTable);
    DROP_TABLE(warpSkyfileDropTable);
    DROP_TABLE(warpMaskDropTable);
    DROP_TABLE(diffRunDropTable);
    DROP_TABLE(diffInputSkyfileDropTable);
    DROP_TABLE(diffSkyfileDropTable);
    DROP_TABLE(stackRunDropTable);
    DROP_TABLE(stackInputSkyfileDropTable);
    DROP_TABLE(stackSumSkyfileDropTable);
    DROP_TABLE(magicRunDropTable);
    DROP_TABLE(magicInputSkyfileDropTable);
    DROP_TABLE(magicTreeDropTable);
    DROP_TABLE(magicNodeResultDropTable);
    DROP_TABLE(magicMaskDropTable);
    DROP_TABLE(magicSkyfileMaskDropTable);
    DROP_TABLE(calDBDropTable);
    DROP_TABLE(calRunDropTable);
    DROP_TABLE(flatcorrRunDropTable);
    DROP_TABLE(flatcorrExpDropTable);
    DROP_TABLE(pstampDataStoreDropTable);
    DROP_TABLE(pstampRequestDropTable);
    DROP_TABLE(pstampJobDropTable);

    // re-enable foreign key constrants
    if (!p_psDBRunQuery(config->dbh, "SET FOREIGN_KEY_CHECKS=1")) {
        psError(PS_ERR_UNKNOWN, false, "database error");
        status = false; 
    }

    return status;
}
