import {gql} from "@apollo/client";
import {getSinglePrimaryKey, getSinglePrimaryKeyCol} from "./CustomeTableUtils";
import {getType} from "./TransformText";
import { snakeCase } from 'change-case';
/*
* Builds get query given table name and cols sorted by primary key
* */
export function getSearchQuery(tablename: string, col: any[]): any {
    let primaryKeyCol = getSinglePrimaryKey(col);
    let cols = col.map(c => snakeCase(c.name));

    let colBody = '{';
    for (let i = 0; i < cols.length; i++) {
        colBody += cols[i];
        if (i !== cols.length - 1) {
            colBody += ',';
        }
    }
    colBody += '}';
    let sortingParams = `(order_by:{${primaryKeyCol}: desc})`;
    let query = gql`query{${tablename}${sortingParams}${colBody}}`
    return query;
}

/*
* Builds subscription query given table name and cols sorted by primary key
* */
export function getSubscriptionQuery(tablename: string, col: any[]): any {
    let primaryKeyCol = getSinglePrimaryKey(col);
    col = col.map(c => snakeCase(c.name));
    let colBody = '{';
    for (let i = 0; i < col.length; i++) {
        colBody += col[i];
        if (i !== col.length - 1) {
            colBody += ',';
        }
    }
    colBody += '}';
    let sortingParams = `(order_by:{${primaryKeyCol}: asc})`;
    let query = gql`subscription{${tablename}${sortingParams}${colBody}}`
    return query;
}

/*
* query for hasura update by primary key
* */
export function updateTableByPrimaryKey(tablename: string, col: any[]): any {
    let singlePrimaryKeyCol = getSinglePrimaryKey(col);
    let updateInputs = `$${singlePrimaryKeyCol}: Int,`;
    for (let i = 0; i < col.length; i++) {
        updateInputs += `$${snakeCase(col[i].name)}: ${getType(col[i].type)}`;
        if (i !== col.length - 1) {
            updateInputs += ',';
        }
    }

    let updateConditions = `{${singlePrimaryKeyCol}: {_eq:$${singlePrimaryKeyCol}}}`;

    let updateBody = `{`;
    for (let i = 0; i < col.length; i++) {
        updateBody += `${snakeCase(col[i].name)}: $${snakeCase(col[i].name)}`;
        if (i !== col.length - 1) {
            updateBody += ',';
        }
    }
    updateBody += `}`
    const updateQuery = gql`mutation updateTable (${updateInputs}) {
        update_${tablename}(where: ${updateConditions}, _set: ${updateBody}) {
          affected_rows
        }
      }`
    return updateQuery;
}

export function addTableRecord(tablename: string, col: any[]): any {
    let singlePrimaryKeyCol = getSinglePrimaryKey(col);
    let insertQuery = gql`mutation insert_single_${tablename}($object: ${tablename}_insert_input!) {
        insert_${tablename}_one(
            object: $object
        ) {
        ${singlePrimaryKeyCol}
        }
    }`;

    return insertQuery;
}

export function deleteRecordByPrimaryKey(tablename: string, col: any[]): any {
    let singlePrimaryKeyCol = getSinglePrimaryKeyCol(col);
    let singlePrimaryKeyColType = singlePrimaryKeyCol.type;
    let singlePrimaryKeyColName = snakeCase(singlePrimaryKeyCol.name);

    let inputParams = `$${singlePrimaryKeyColName}: ${singlePrimaryKeyColType}`;
    let conditions = `${singlePrimaryKeyColName}: $${singlePrimaryKeyColName}`;

    let deleteQuery = gql`
    mutation delete_an_object(${inputParams}) {
        delete_${tablename}_by_pk (
        ${conditions}
        ) {
        ${singlePrimaryKeyColName}
        }
        }
    `

    return deleteQuery;
}
