<?php

/**
* Get all investment
*
* @param $args array
*
* @return array
*/
function esi_get_all_investment( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'number'     => 20,
        'offset'     => 0,
        'orderby'    => 'id',
        'order'      => 'ASC',
    );

    $args      = wp_parse_args( $args, $defaults );
    $cache_key = 'investment-all';
    $items     = wp_cache_get( $cache_key, 'esi' );

    if ( false === $items ) {
        $items = $wpdb->get_results( 'SELECT ' . $wpdb->prefix . 'esi_investment.*, entity.name AS entity, investment_type.name AS investment_type  FROM ' . $wpdb->prefix . 'esi_investment
        LEFT JOIN ' . $wpdb->prefix . 'esi_entity  AS entity ON ' . $wpdb->prefix . 'esi_investment.entity_id = entity.id
        LEFT JOIN ' . $wpdb->prefix . 'esi_investment_type  AS investment_type ON ' . $wpdb->prefix . 'esi_investment.investment_type_id = investment_type.id
        ORDER BY ' . $wpdb->prefix . 'esi_investment.' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );

        foreach ($items as $item) {
            $item->investor = array_map(function($val) {
                return $val->name;
            }, esi_get_investor($item->id));
        }
        wp_cache_set( $cache_key, $items, 'esi' );
    }
    return $items;
}

/**
* Fetch all investment from database
*
* @return array
*/
function esi_get_investment_count() {
    global $wpdb;

    return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'esi_investment' );
}

/**
* Fetch a single investment from database
*
* @param int   $id
*
* @return array
*/
function esi_get_investment( $id = 0 ) {
    global $wpdb;

    return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'esi_investment WHERE id = %d', $id ) );
}

/**
* Insert a investment investment
*
* @param array $args
*/
function esi_insert_investment( $args = array() ) {
    global $wpdb;

    $defaults = array(
        'id'         => null,
        'entity_id' => '',
        'investment_type_id' => '',
        'amount' => '',
        'investment_date' => '',
        'investor' => array(),
    );

    $args       = wp_parse_args( $args, $defaults );
    $table_name = $wpdb->prefix . 'esi_investment';

    // some basic validation
    if ( empty( $args['entity_id'] ) ) {
        return new WP_Error( 'no-entity-id', __( 'No entity provided.', 'esi' ) );
    }
    if ( empty( $args['investment_type_id'] ) ) {
        return new WP_Error( 'no-investment-type-id', __( 'No investment type provided.', 'esi' ) );
    }
    if ( empty( $args['amount'] ) ) {
        return new WP_Error( 'no-amount', __( 'No amount provided.', 'esi' ) );
    }
    if ( empty( $args['investment_date'] ) ) {
        return new WP_Error( 'no-investment-date', __( 'No investment date provided.', 'esi' ) );
    }
    if ( empty( $args['investor'] ) ) {
        return new WP_Error( 'no-investor', __( 'No investor provided.', 'esi' ) );
    }

    // remove row id to determine if investment or update
    $row_id = (int) $args['id'];
    unset( $args['id'] );
    $investors = $args['investor'];
    unset( $args['investor'] );

    if ( ! $row_id ) {
        // insert a investment
        if ( $wpdb->insert( $table_name, $args ) ) {
            $row_id = $wpdb->insert_id;
            esi_insert_investor($row_id, $investors);
            return $row_id;
        }

    } else {

        esi_insert_investor($row_id, $investors);
        // do update method here
        if ( $wpdb->update( $table_name, $args, array( 'id' => $row_id ) ) ) {
            return $row_id;
        }
    }

    return false;
}

/**
* Deletitem
*
* @param int   $id
*
*/
function esi_delete_investment( $id = 0 ) {
    global $wpdb;
    $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'esi_investment WHERE id = %d', $id ) );
}

/**
* Fetch Investor radar array
*
* @param int   $id
*
* @return array
*/
function esi_get_investor( $id = 0 ) {
    global $wpdb;

    // Get Selected Investor mapping
    $items = $wpdb->get_results('SELECT * FROM ' .$wpdb->prefix . 'esi_investor
    LEFT JOIN ' . $wpdb->prefix . 'esi_entity  AS entity ON ' . $wpdb->prefix . 'esi_investor.entity_id = entity.id
    WHERE investment_id='.$id);
    if($items){
        return $items;
    }
    return array();
}


/**
* Fetch Investor radar array
*
* @param int   $id
*
* @return array
*/
function esi_get_investor_array( $id = 0 ) {
    global $wpdb;

    // Get Selected Investor mapping
    $arrSelectedInvestor = $wpdb->get_results("SELECT entity_id FROM " .$wpdb->prefix . "esi_investor WHERE investment_id=".$id);
    if($arrSelectedInvestor){
        return array_map(function($val) {
            return intval($val->entity_id);
        }, $arrSelectedInvestor);
    }else{
        return array();
    }
}

function esi_insert_investor( $investmentId, $args = array() ) {
    global $wpdb;

    $wpdb->delete( $wpdb->prefix . "esi_investor", array( 'investment_id' => $investmentId ) );
    foreach ($args as $key => $val) {
        $wpdb->insert(
        $wpdb->prefix . "esi_investor",
        array(
            'investment_id' => intval($investmentId),
            'entity_id' => intval($val)
        ));
    }
}
